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!
#include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; struct node{ int x,y,z,s; }; int next[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}}; int n,m,k,endx,endy,endz,flag; char e[35][35][35]; int bfs(int a,int b,int c){ struct node t1,t2,t3; queue<node> q; t1.x=a;t1.y=b;t1.z=c;t1.s=0; q.push(t1); while(!q.empty()){ t2=q.front();q.pop(); for(int i=0;i<6;i++){ t3.x=t2.x+next[i][0]; t3.y=t2.y+next[i][1]; t3.z=t2.z+next[i][2]; t3.s=t2.s+1; if(t3.x<0 || t3.x>=n || t3.y<0 || t3.y>=m || t3.z<0 || t3.z>=k) continue; if(e[t3.x][t3.y][t3.z]!='#'){ if(t3.x==endx && t3.y==endy && t3.z==endz){ flag=1; return t3.s; } q.push(t3); e[t3.x][t3.y][t3.z]='#'; } } } } int main(){ int startx,starty,startz; while(cin >> n >> m >> k){ if(n==0 && m==0 && k==0) break; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ for(int r=0;r<k;r++){ cin >> e[i][j][r]; if(e[i][j][r]=='S'){ startx=i;starty=j;startz=r; } if(e[i][j][r]=='E'){ endx=i;endy=j;endz=r; } } } } flag=0; int sum=bfs(startx,starty,startz); if(flag==1) printf("Escaped in %d minute(s).\n",sum); else cout << "Trapped!" << endl; } return 0; }