Escaped in x minute(s).
Trapped!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <stack> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <ctime> 12 #define LL long long 13 #define INF 0x3f3f3f3f 14 #define pii pair<int,int> 15 16 using namespace std; 17 const int maxn = 40; 18 char table[maxn][maxn][maxn]; 19 int L,R,C,sx,sy,sz,ex,ey,ez; 20 bool vis[maxn][maxn][maxn]; 21 struct node { 22 int x,y,z,step; 23 node(int tx = 0,int ty = 0,int tz = 0,int tp = 0) { 24 x = tx; 25 y = ty; 26 z = tz; 27 step = tp; 28 } 29 }; 30 const int dir[6][3] = { 31 {0,0,-1},//left 32 {0,0,1},//right 33 {0,-1,0},//up 34 {0,1,0},//down 35 {-1,0,0},//front 36 {1,0,0}//back 37 }; 38 bool isIn(const node &tmp) { 39 int a = (tmp.x >= 0 && tmp.x < L); 40 int b = (tmp.y >= 0 && tmp.y < R); 41 int c = (tmp.z >= 0 && tmp.z < C); 42 return a + b + c == 3; 43 } 44 queue<node>q; 45 int bfs() { 46 while(!q.empty()) q.pop(); 47 q.push(node(sx,sy,sz,0)); 48 vis[sx][sy][sz] = true; 49 while(!q.empty()) { 50 node now = q.front(); 51 q.pop(); 52 if(table[now.x][now.y][now.z] == 'E') return now.step; 53 for(int i = 0; i < 6; ++i) { 54 node tmp(now.x+dir[i][0],now.y+dir[i][1],now.z+dir[i][2],now.step+1); 55 if(isIn(tmp)&&table[tmp.x][tmp.y][tmp.z] != '#' &&!vis[tmp.x][tmp.y][tmp.z]) { 56 vis[tmp.x][tmp.y][tmp.z] = true; 57 if(table[tmp.x][tmp.y][tmp.z] == 'E') return tmp.step; 58 q.push(tmp); 59 } 60 } 61 } 62 return -1; 63 } 64 int main() { 65 while(scanf("%d %d %d",&L,&R,&C),L||R||C) { 66 memset(table,'\0',sizeof(table)); 67 memset(vis,false,sizeof(vis)); 68 for(int i = 0; i < L; ++i) { 69 for(int j = 0; j < R; ++j) { 70 scanf("%s",table[i][j]); 71 for(int k = 0; k < C; ++k) 72 if(table[i][j][k] == 'S') { 73 sx = i; 74 sy = j; 75 sz = k; 76 } 77 } 78 } 79 int ans = bfs(); 80 if(~ans) printf("Escaped in %d minute(s).\n",ans); 81 else puts("Trapped!"); 82 } 83 return 0; 84 }