1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include<iostream> using namespace std; int L, R, C, ans; int dx[6] = {1, -1, 0, 0, 0, 0}; int dy[6] = {0, 0, 1, -1, 0, 0}; int dz[6] = {0, 0, 0, 0, 1, -1}; char arr[35][35][35]; bool vis[35][35][35];
struct dot{ int x, y, z; int dis; dot(int X, int Y, int Z):x{X}, y{Y}, z{Z}, dis{0} {} dot(): x{-1}, y{-1}, z{-1}, dis{0} {} };
dot start, End, cur, nxt;
int BFS(){ dot que[50000]; que[0] = start; vis[start.x][start.y][start.z] = true; for(int i=0, j=1 ; i<j ; i++){ cur = que[i]; if(cur.x == End.x && cur.y == End.y && cur.z == End.z){ return cur.dis; } for(int k=0 ; k<6 ; k++){ nxt = cur; nxt.x += dx[k]; nxt.y += dy[k]; nxt.z += dz[k]; if(nxt.x < 0 || nxt.x >= L || nxt.y < 0 || nxt.y >= R || nxt.z < 0 || nxt.z >= C) continue; if(!vis[nxt.x][nxt.y][nxt.z] && arr[nxt.x][nxt.y][nxt.z] != '#'){ vis[nxt.x][nxt.y][nxt.z] = true; nxt.dis = cur.dis + 1; que[j] = nxt; j++; } } } return -1; }
int main(){ while(cin>>L>>R>>C && (L!=0 && R!=0 && C!=0)){ for(int i=0 ; i<L ; i++){ for(int j=0 ; j<R ; j++){ for(int k=0 ; k<C ; k++){ cin>>arr[i][j][k]; vis[i][j][k] = false; if(arr[i][j][k] == 'S'){ start = dot(i, j, k); } else if(arr[i][j][k] == 'E'){ End = dot(i, j, k); } } } } ans = BFS(); if(ans == -1){ cout<<"Trapped!\n"; } else{ cout<<"Escaped in "<<ans<<" minute(s).\n"; } }
return 0; }
|