Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24602 | Accepted: 9525 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
3d bfs//原来不知道哪里错了盯着代码debug半天没结果 重新手敲一遍就a了- -
ACcode:
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #define maxn 33 using namespace std; char mapp[maxn][maxn][maxn]; bool vis[maxn][maxn][maxn]; int to[][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; int n,m,l; struct N{ int x,y,z,t; }; N my,no,ne; bool ju(N a){ if(a.x<0||a.x>=n||a.y<0||a.y>=m||a.z<0||a.z>=l||mapp[a.x][a.y][a.z]=='#'||vis[a.x][a.y][a.z])return 0; return 1; } void bfs(){ memset(vis,false,sizeof(vis)); queue<N>q; q.push(my); vis[my.x][my.y][my.z]=1; while(!q.empty()){ no=q.front();q.pop(); for(int i=0;i<6;++i){ ne=no; ne.x+=to[i][0]; ne.y+=to[i][1]; ne.z+=to[i][2]; ne.t++; if(ju(ne)){ if(mapp[ne.x][ne.y][ne.z]=='E'){ printf("Escaped in %d minute(s).\n",ne.t); return; } q.push(ne); vis[ne.x][ne.y][ne.z]=1; } } } printf("Trapped!\n"); } int main(){ while(~scanf("%d%d%d",&n,&m,&l)&&(n+m+l)){ for(int i=0;i<n;++i) for(int j=0;j<m;++j) for(int z=0;z<l;++z){ cin>>mapp[i][j][z]; if(mapp[i][j][z]=='S'){ my.x=i; my.y=j; my.z=z; my.t=0; } } bfs(); } }