#include <iostream> #include <queue> using namespace std;
struct coordinate { int x, y, z; coordinate() { x = 0; y = 0; z = 0; } coordinate(int _x, int _y, int _z) :x(_x), y(_y), z(_z) { } coordinate(const coordinate& c) { x = c.x; y = c.y; z = c.z; } bool operator!=(const coordinate& C) { return (x != C.x || y != C.y || z != C.z); } bool operator==(const coordinate& C) { return (x == C.x || y == C.y || z == C.z); } };
const int maxN = 35; char room[maxN][maxN][maxN];
int dX[] = { -1,1,0,0,0,0 }; int dY[] = { 0,0,-1,1,0,0 }; int dZ[] = { 0,0,0,0,-1,1 };
int maze(int l, int r, int c, coordinate S, coordinate E) { bool reached[maxN][maxN][maxN] = { false }; queue<coordinate> q; queue<int> steps; q.push(S); steps.push(0); while (!q.empty()) { coordinate cur = q.front(); q.pop(); int stp = steps.front(); steps.pop(); int cx = cur.x, cy = cur.y, cz = cur.z; for (int i = 0; i < 6; i++) { int tx = cx + dX[i]; int ty = cy + dY[i]; int tz = cz + dZ[i]; if (tx >= 0 && ty >= 0 && tz >= 0 && tx < l && ty < r && tz < c && !reached[tx][ty][tz] && room[tx][ty][tz] != '#') { reached[tx][ty][tz] = true; if (room[tx][ty][tz] == 'E') { return (stp + 1); break; } coordinate tempC(tx, ty, tz); q.push(tempC); steps.push(stp + 1); } } } return 0; }
int main() { int l, r, c; while (!(cin >> l >> r >> c).eof()) { if (l == 0 && r == 0 && c == 0) break; coordinate st, ed; for (int i = 0; i < l; i++) for (int j = 0; j < r; j++) for (int k = 0; k < c; k++) { cin >> room[i][j][k]; if (room[i][j][k] == 'S') { coordinate tempC(i, j, k); st = tempC; } if (room[i][j][k] == 'E') { coordinate tempC(i, j, k); ed = tempC; } } int res = maze(l, r, c, st, ed); if (res != 0) cout << "Escaped in " << res << " minute(s)." << endl; else cout << "Trapped!" << endl; }
return 0; }
|