标签:
额,简单的BFS,一开始DFS结果栈炸了,加栈也不行 QAQ
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100; const int dir[6][3] ={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; #define FOR(i,n) for(int i = 0; i < n; i++) int mat[maxn][maxn][maxn],vis[maxn][maxn][maxn]; int n,m,k,s,v,a,b,c,area,ans; bool judge(int x,int y,int z){ if(x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k) return true; return false; } void dfs(int x,int y,int z){ queue<int>qx; queue<int>qy; queue<int>qz; qx.push(x); qy.push(y); qz.push(z); vis[x][y][z] = 1; while(!qx.empty()){ int px = qx.front(),py = qy.front(), pz = qz.front(); qx.pop(); qy.pop(); qz.pop(); for(int i = 0; i < 6; i++){ int xx = px + dir[i][0], yy = py + dir[i][1], zz = pz + dir[i][2]; if(judge(xx,yy,zz) && !mat[xx][yy][zz] && !vis[xx][yy][zz]){ vis[xx][yy][zz] = 1; qx.push(xx); qy.push(yy); qz.push(zz); } } } return; } int main(){ while(scanf("%d%d%d%d",&n,&m,&k,&s)){ if(!n && !m && !k && !s) break; area = n * m; ans = 0; memset(mat,0,sizeof(mat)); memset(vis,0,sizeof(vis)); FOR(i,s){ scanf("%d",&v); c = v / area; v %= area; a = v % n; b = v / n; mat[a][b][c] = 1; } FOR(i,n)FOR(j,m){ if(!vis[i][j][k - 1] && !mat[i][j][k - 1]) dfs(i,j,k - 1); if(!vis[i][j][0] && !mat[i][j][0]) dfs(i,j,0); } FOR(i,n)FOR(j,k){ if(!vis[i][m - 1][j] && !mat[i][m - 1][j]) dfs(i,m - 1,j); if(!vis[i][0][j] && !mat[i][0][j]) dfs(i,0,j); } FOR(i,m)FOR(j,k){ if(!vis[0][i][j] && !mat[0][i][j]) dfs(0,i,j); if(!vis[n - 1][i][j] && !mat[n - 1][i][j]) dfs(n - 1,i,j); } FOR(i,n)FOR(j,m)FOR(h,k) if(mat[i][j][h]){ for(int d = 0; d < 6; d++){ int ii = i + dir[d][0]; int jj = j + dir[d][1]; int hh = h + dir[d][2]; if(judge(ii,jj,hh)){ if(vis[ii][jj][hh]) ans++; } else ans ++; } } printf("The number of faces needing shielding is %d.\n",ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/u013451221/article/details/45200461