#include <iostream> #include <cstring> #include <queue> #include <cstdio> using namespace std; int c, n, m; const int maxn = 35; char mp[maxn][maxn][maxn]; int dz[6] = {0, 0, 0, 0, 1, -1}; int dx[6] = {1, -1, 0, 0, 0, 0}; int dy[6] = {0, 0, 1, -1, 0, 0}; int sx, sy, sz; int tx, ty, tz; bool vis[maxn][maxn][maxn]; struct Node{ int x, y, z; int tot; Node(int a, int b, int c, int num):x(a), y(b), z(c), tot(num){}; }; bool judge(int x, int y, int z){ if(vis[z][x][y] == true) return false; if(x<0||x>=n) return false; if(y<0||y>=m) return false; if(z<0||z>=c) return false; if(mp[z][x][y] == '#') return false; if(z == sz&&x == sx&&y == sy) return false; return true; } int bfs(int x, int y, int z){ Node temp = Node{x, y, z, 0}; queue<Node> q; q.push(temp); vis[z][x][y] = true; while(!q.empty()){ Node temp1 = q.front(); q.pop(); if(temp1.x == tx&&temp1.y == ty&&temp1.z == tz){ return temp1.tot; } for(int i=0; i<6; i++){ int nx = temp1.x+dx[i]; int ny = temp1.y+dy[i]; int nz = temp1.z+dz[i]; int step = temp1.tot+1; if(judge(nx, ny, nz)){ vis[nz][nx][ny] = true; q.push(Node{nx, ny, nz, step}); } } } return -1; } int main() { while(cin>>c>>n>>m&&(n+m+c)!=0){ getchar(); memset(vis, 0, sizeof(vis)); for(int i=0; i<c; i++){ for(int j=0; j<n; j++){ for(int k=0; k<m; k++){ cin>>mp[i][j][k]; if(mp[i][j][k] == 'S'){ sz = i, sx = j, sy = k; //cout<<sz<<" "<<sx<<" "<<sy<<endl; } else if(mp[i][j][k] == 'E'){ tz = i, tx = j, ty = k; //cout<<tz<<" "<<tx<<" "<<ty<<endl; } } } } int tot = bfs(sx, sy, sz); if(tot!=-1){ printf("Escaped in %d minute(s).\n", tot); } else printf("Trapped!\n"); } return 0; }
|