这题并不难,一个三维BFS,不过很难AC,易错点写在注释里。
#include<iostream>
#include<cstring>
using namespace std;
typedef struct node
{
int x;
int y;
int z;
}Node;
Node que[1300*130*70];
int m,n,l,t;
bool map[1300][130][70];
char book[1300][130][70];
int dx[6]={-1, 0, 1, 0, 0, 0};
int dy[6]={ 0, 1, 0,-1, 0, 0};
int dz[6]={ 0, 0, 0, 0, 1,-1};
int bfs(int x, int y, int z)
{
int head, tail;
head=tail=0;
que[tail].x=x;
que[tail].y=y;
que[tail].z=z;
tail++;
book[x][y][z]=1;
int i,xx,yy,zz;
while(head<tail)
{
for(i=0; i<6; i++)
{
xx=que[head].x+dx[i];
yy=que[head].y+dy[i];
zz=que[head].z+dz[i];
if (xx<1||xx>m||yy<1||yy>n||zz<1||zz>l)
continue;
if( map[xx][yy][zz] && book[xx][yy][zz]==0)
{
que[tail].x=xx;
que[tail].y=yy;
que[tail].z=zz;
book[xx][yy][zz]=1;
tail++;
}
}
head++;
}
return tail;
}
int main()
{
int i,j,k,head,tail,tx,ty,tz,max=0,flag;
cin>>m>>n>>l>>t;
memset(book,0,sizeof(book));
for (k=1;k<=l;k++)
{
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
scanf("%d",&map[i][j][k]);
}
}
for (k=1;k<=l;k++)
{
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
if (map[i][j][k]&&book[i][j][k]==0)
{
flag=bfs(i,j,k);
if (flag>=t)
max+=flag;
}
}
}
}
cout<<max;
}