PAT (Advanced Level) 1091. Acute Stroke (30)
BFS求连通块。递归会爆栈。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; int M,N,L,T; int A[70][1300][133]; bool f[70][1300][133]; int dir[10][5]={ {0,0,1}, {0,0,-1}, {0,1,0}, {0,-1,0}, {1,0,0}, {-1,0,0} }; struct Point { int a,b,c; Point (int aa,int bb,int cc) { a=aa; b=bb; c=cc; } }; int Find(int a,int b,int c) { int res=0; queue<Point>q; Point p(a,b,c); q.push(p); f[a][b][c]=1; while(!q.empty()) { res++; Point head=q.front(); q.pop(); for(int i=0;i<6;i++) { int na=head.a+dir[i][0]; int nb=head.b+dir[i][1]; int nc=head.c+dir[i][2]; if(na>L||na<1) continue; if(nb>M||nb<1) continue; if(nc>N||nc<1) continue; if(A[na][nb][nc]==0) continue; if(f[na][nb][nc]==1) continue; Point p(na,nb,nc); q.push(p); f[na][nb][nc]=1; } } return res; } int main() { memset(A,0,sizeof A); memset(f,0,sizeof f); scanf("%d%d%d%d",&M,&N,&L,&T); for(int k=1;k<=L;k++) for(int i=1;i<=M;i++) for(int j=1;j<=N;j++) scanf("%d",&A[k][i][j]); int sum=0; for(int k=1;k<=L;k++) for(int i=1;i<=M;i++) for(int j=1;j<=N;j++) { if(A[k][i][j]==0) continue; if(f[k][i][j]==1) continue; int res=Find(k,i,j); if(res>=T) sum=sum+res; } printf("%d\n",sum); return 0; }