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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#include <iostream> #include <string.h> #include <vector> #include <cstdio> #include <queue> #define ll long long #define INF 100000000 using namespace std; int gr[32][32][32]; bool visit[32][32][32]; int dist[32][32][32]; int x,y,z; int ex,ey,ez; int L,R,C; void bfs() { int tmp_x,tmp_y,tmp_z; queue<int> q; q.push(x); q.push(y); q.push(z); visit[x][y][z]=1; dist[x][y][z] = 0; while(!q.empty()) { tmp_x = q.front(); q.pop(); tmp_y = q.front(); q.pop(); tmp_z = q.front(); q.pop(); if(tmp_z+1<L && !visit[tmp_x][tmp_y][tmp_z+1] && gr[tmp_x][tmp_y][tmp_z+1]!=4) { visit[tmp_x][tmp_y][tmp_z+1]=1; dist[tmp_x][tmp_y][tmp_z+1] = min(dist[tmp_x][tmp_y][tmp_z+1],dist[tmp_x][tmp_y][tmp_z]+1); q.push(tmp_x); q.push(tmp_y); q.push(tmp_z+1); } if(tmp_z-1>=0 && !visit[tmp_x][tmp_y][tmp_z-1] && gr[tmp_x][tmp_y][tmp_z-1]!=4) { visit[tmp_x][tmp_y][tmp_z-1]=1; dist[tmp_x][tmp_y][tmp_z-1] = min(dist[tmp_x][tmp_y][tmp_z-1],dist[tmp_x][tmp_y][tmp_z]+1); q.push(tmp_x); q.push(tmp_y); q.push(tmp_z-1); } if(tmp_x+1<R && !visit[tmp_x+1][tmp_y][tmp_z] && gr[tmp_x+1][tmp_y][tmp_z]!=4) { visit[tmp_x+1][tmp_y][tmp_z]=1; dist[tmp_x+1][tmp_y][tmp_z] = min(dist[tmp_x+1][tmp_y][tmp_z],dist[tmp_x][tmp_y][tmp_z]+1); q.push(tmp_x+1); q.push(tmp_y); q.push(tmp_z); } if(tmp_x-1>=0 && !visit[tmp_x-1][tmp_y][tmp_z] && gr[tmp_x-1][tmp_y][tmp_z]!=4) { visit[tmp_x-1][tmp_y][tmp_z]=1; dist[tmp_x-1][tmp_y][tmp_z] = min(dist[tmp_x-1][tmp_y][tmp_z],dist[tmp_x][tmp_y][tmp_z]+1); q.push(tmp_x-1); q.push(tmp_y); q.push(tmp_z); } if(tmp_y-1>=0 && !visit[tmp_x][tmp_y-1][tmp_z] && gr[tmp_x][tmp_y-1][tmp_z]!=4) { visit[tmp_x][tmp_y-1][tmp_z]=1; dist[tmp_x][tmp_y-1][tmp_z] = min(dist[tmp_x][tmp_y-1][tmp_z],dist[tmp_x][tmp_y][tmp_z]+1); q.push(tmp_x); q.push(tmp_y-1); q.push(tmp_z); } if(tmp_y+1<C && !visit[tmp_x][tmp_y+1][tmp_z] && gr[tmp_x][tmp_y+1][tmp_z]!=4) { q.push(tmp_x); q.push(tmp_y+1); q.push(tmp_z); visit[tmp_x][tmp_y+1][tmp_z]=1; dist[tmp_x][tmp_y+1][tmp_z]=min(dist[tmp_x][tmp_y+1][tmp_z],dist[tmp_x][tmp_y][tmp_z]+1); } } } int main() { char c; while(cin>>L) { cin>>R>>C; if(L+R+C==0) break; for(int i=0;i<L;i++) { for(int j=0;j<R;j++) { for(int k=0;k<C;k++) { dist[j][k][i]=INF; visit[j][k][i]=0; cin>>c; if(c=='S') { gr[j][k][i]=0;//src = 0 x=j;y=k;z=i;//R,C,L } if(c=='E') { gr[j][k][i]=2;//exit = 2; ex=j;ey=k;ez=i; } if(c=='.') { gr[j][k][i]=1;//can move =1 } if(c=='#') { gr[j][k][i]=4;//cant move = 4 } }//1st for loop }//2nd for loop getchar(); }//3rd for loop bfs(); if(dist[ex][ey][ez]==INF) { cout<<"Trapped!\n"; } else { cout<<"Escaped in "; cout<<dist[ex][ey][ez]<<" minute(s).\n"; } } return 0; } |