#include<iostream>
#include<queue>
using namespace std;
struct Node{
int x,y,z;
};
int m,n,l,t;
int a[61][1287][129]={};
bool vis[61][1287][129]={false};
int _x[6]={1,0,0,-1,0,0};
int _y[6]={0,1,0,0,-1,0};
int _z[6]={0,0,1,0,0,-1};
bool judge(int x,int y,int z){
if(x>=l||y>=m||z>=n||x<0||y<0||z<0) return false;
else if(a[x][y][z]==0||vis[x][y][z]==true) return false;
else return true;
}
int bfs(int xx,int yy,int zz){
queue<Node> q;
vis[xx][yy][zz]=true;
Node temp;
temp.x=xx;
temp.y=yy;
temp.z=zz;
q.push(temp);
int cnt=0;
while(!q.empty()){
Node now=q.front();
q.pop();
cnt++;
for(int i=0;i<6;i++){
Node t;
t.x=now.x+_x[i];t.y=now.y+_y[i];t.z=now.z+_z[i];
if(judge(t.x,t.y,t.z)){
vis[t.x][t.y][t.z]=true;
q.push(t);
}
}
}
if(cnt>=t){
return cnt;
}else return 0;
}
int main(){
/*#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif*/
//cin>>m>>n>>l>>t;
scanf("%d %d %d %d",&m,&n,&l,&t);
for(int s=0;s<l;s++){
for(int r=0;r<m;r++){
for(int c=0;c<n;c++){
//cin>>a[s][r][c];
scanf("%d",&a[s][r][c]);
}
}
}
int ans=0;
for(int s=0;s<l;s++){
for(int r=0;r<m;r++){
for(int c=0;c<n;c++){
if(vis[s][r][c]==false&&a[s][r][c]==1){
ans+=bfs(s,r,c);
}
}
}
}
//cout<<ans<<endl;
printf("%d",ans);
return 0;
}
本来打算暴力把所有的情况都列出来的,后来写到第三个就写不下去了,掐指一算要写十几个判断语句。。。参考了https://www.liuchuo.net/archives/2307的文章,采用bfs解决问题着实优秀,但是后来发现cin,cout两个超时,改成scanf和printf之后还有一个超时,再把平时常用的文件流删掉就AC了。