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
| #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int N = 32;
int L, R, C, sx, sy, sz; char a[N][N][N]; int vis[N][N][N];
struct node {int x, y, z, cnt;}; int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
inline void bfs() { queue<node>Q; node t; vis[sx][sy][sz] = 1; t.x = sx, t.y = sy, t.z = sz, t.cnt = 0; Q.push(t); while (Q.size()) { node nows = Q.front(); Q.pop(); int nx = nows.x, ny = nows.y, nz = nows.z, ncnt = nows.cnt; if (a[nx][ny][nz] == 'E') { cout << "Escaped in " << ncnt << " minute(s)." << '\n'; return; } for (int i = 0; i < 6; ++ i) { node T; T.x = nx + dir[i][0], T.y = ny + dir[i][1], T.z = nz + dir[i][2]; T.cnt = ncnt + 1; if (T.x > L || T.y > R || T.z > C || T.x < 1 || T.y < 1 || T.z < 1) continue; if (a[T.x][T.y][T.z] != '#' && ! vis[T.x][T.y][T.z]) { vis[T.x][T.y][T.z] = 1; Q.push(T); } } } puts("Trapped!"); return; }
signed main() { while (scanf ("%d%d%d", &L, &R, &C)) { if (L == 0 && R == 0 && C == 0) return 0; int k, i, j; memset(vis, 0, sizeof vis); for (k = 1; k <= L; ++ k) { for (i = 1; i <= R; ++ i) { for (j = 1; j <= C; ++ j) { cin >> a[k][i][j]; if (a[k][i][j] == 'S') sx = k, sy = i, sz = j; } } } bfs(); } return 0; }
|