原题链接
广度优先探索 还是比较有套路的
我错主要是错在了cnt++的位置
要在弹出去之前cnt++ 还有要注意深度防爆栈inq[1290][130][61]
AC代码:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 1290;
struct node
{
int x, y, z;
}Node;
int X[6] = {0,0,0,0,1,-1};
int Y[6] = {0,0,-1,1,0,0};
int Z[6] = {1,-1,0,0,0,0};
int m, n, clice, t, ans = 0;
bool inq[1290][130][61] = {false}; //判断是否在队列里
int pixel[1290][130][61];
bool judge(int x, int y, int z) //n行m列lice层
{
if(x >= n || x < 0 || y >= m || y < 0 || z < 0 || z >= clice) return false;
if(pixel[x][y][z] == 0 || inq[x][y][z]) return false;
return true;
}
int BFS(int x, int y, int z) //进队,如队列
{
int cnt = 0;
queue<node> q; //定义队列
Node.x = x;
Node.y = y;
Node.z = z;
q.push(Node);
inq[x][y][z] = true;
while(!q.empty())
{
node top = q.front();
q.pop();
cnt++; //当前块中1的个数加一
for(int i = 0; i < 6; i++)
{
int newx = top.x + X[i];
int newy = top.y + Y[i];
int newz = top.z + Z[i];
if(judge(newx,newy,newz))
{
Node.x = newx;
Node.y = newy;
Node.z = newz;
q.push(Node);
inq[newx][newy][newz] = true;
}
}
}
return cnt;
}
int main()
{
int ans = 0;
cin >> n >> m >> clice >> t;
for(int z = 0; z < clice; z++)
{
for(int x = 0; x < n; x++)
{
for(int y = 0; y < m; y++)
{
cin >> pixel[x][y][z];
}
}
}
int temp;
for(int z = 0; z < clice; 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)
{
temp = BFS(x,y,z);
if(temp >= t) ans += temp;
}
}
}
}
cout << ans;
return 0;
}