记录一个菜逼的成长。。
题目链接
这题原来写的是dfs。结果给我段错误,应该是爆栈了?
后来改成了三维bfs
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long LL;
int g[1300][130][70];
int n,m,h,T;
int dx[] = {-1,0,1,0,0,0},dy[] = {0,1,0,-1,0,0},dz[] = {0,0,0,0,1,-1};
int bfs(int x,int y,int z)
{
queue<int>qx,qy,qz;
qx.push(x),qy.push(y),qz.push(z);
int area = 0;g[x][y][z] = 0;
while(!qx.empty()){
int fx = qx.front(),fy = qy.front(),fz = qz.front();
area++;
qx.pop();qy.pop();qz.pop();
for( int i = 0; i < 6; i++ ){
int nx = fx + dx[i],ny = fy + dy[i],nz = fz + dz[i];
if(nx < 1 || nx > n || ny < 1 || ny > m || nz < 1 || nz > h)continue;
if(!g[nx][ny][nz])continue;
g[nx][ny][nz] = 0;
qx.push(nx),qy.push(ny),qz.push(nz);
}
}
return area;
}
int main()
{
cin>>n>>m>>h>>T;
for( int i = 1; i <= h; i++ ){
for( int j = 1; j <= n; j++ ){
for( int k = 1; k <= m; k++ ){
cin>>g[j][k][i];
}
}
}
int ans = 0;
for( int i = 1; i <= h; i++ ){
for( int j = 1; j <= n; j++ ){
for( int k = 1; k <= m; k++ ){
if(g[j][k][i]){
int ret = bfs(j,k,i);
if(ret >= T)ans += ret;
}
}
}
}
cout<<ans<<endl;
return 0;
}