一道比较简单的30分的题目,经典的bfs,skr。
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cstdio>
#include <map>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct node{
int x,y,z;
}Node;
int n,m,slice,t;
int pixel[1290][130][61];
int inq[1290][130][61] = {0};
int xx[6] = {0,0,0,0,1,-1};
int yy[6] = {0,0,1,-1,0,0};
int zz[6] = {1,-1,0,0,0,0};
int judge(int x,int y,int z)
{
if(x>=n||x<0||y>=m||y<0||z>=slice||z<0)
return 0;
if(pixel[x][y][z]==0||inq[x][y][z]==1)
return 0;
return 1;
}
int bfs(int x,int y,int z)
{
int tot=0;
queue<node> Q;
Node.x=x,Node.y=y,Node.z=z;
Q.push(Node);
inq[x][y][z]=1;
while(!Q.empty())
{
node top=Q.front();
Q.pop();
tot++;
for(int i=0;i<6;i++)
{
if(judge(top.x+xx[i],top.y+yy[i],top.z+zz[i])==1)
{
Node.x=top.x+xx[i];
Node.y=top.y+yy[i];
Node.z=top.z+zz[i];
Q.push(Node);
inq[top.x+xx[i]][top.y+yy[i]][top.z+zz[i]]=1;
}
}
}
if(tot >= t)
return tot;
else
return 0;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&slice,&t);
for(int z=0;z<slice;z++){
for(int x =0;x<n;x++){
for(int y=0;y<m;y++)
scanf("%d",&pixel[x][y][z]);
}
}
int ans=0;
for(int z=0;z<slice;z++){
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
if(pixel[x][y][z]==1&&inq[x][y][z]==false)
ans += bfs(x,y,z);
}
}
}
printf("%d\n", ans);
return 0;
}