|
| 1 | +/* |
| 2 | + Idea: |
| 3 | + - Simple BFS using 3D state. |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <bits/stdc++.h> |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +bool vis[30][30][30]; |
| 11 | +char g[30][30][30]; |
| 12 | +int l, r, c, sl, sr, sc, el, er, ec; |
| 13 | +int dl[] = {1, -1}; |
| 14 | +int dx[] = {0, 1, 0, -1}; |
| 15 | +int dy[] = {1, 0, -1, 0}; |
| 16 | +queue<pair<int, pair<int, int> > > q; |
| 17 | + |
| 18 | +int BFS() { |
| 19 | + memset(vis, false, sizeof vis); |
| 20 | + while(!q.empty()) |
| 21 | + q.pop(); |
| 22 | + |
| 23 | + vis[sl][sr][sc] = true; |
| 24 | + q.push(make_pair(sl, make_pair(sr, sc))); |
| 25 | + |
| 26 | + int cost = 0; |
| 27 | + while(!q.empty()) { |
| 28 | + int size = q.size(); |
| 29 | + |
| 30 | + while(size-- != 0) { |
| 31 | + int cl = q.front().first; |
| 32 | + int cr = q.front().second.first; |
| 33 | + int cc = q.front().second.second; |
| 34 | + q.pop(); |
| 35 | + |
| 36 | + if(g[cl][cr][cc] == 'E') |
| 37 | + return cost; |
| 38 | + |
| 39 | + for(int i = 0, ncl; i < 2; ++i) { |
| 40 | + ncl = cl + dl[i]; |
| 41 | + |
| 42 | + if(ncl < 0 || ncl >= l || g[ncl][cr][cc] == '#' || vis[ncl][cr][cc]) |
| 43 | + continue; |
| 44 | + |
| 45 | + vis[ncl][cr][cc] = true; |
| 46 | + q.push(make_pair(ncl, make_pair(cr, cc))); |
| 47 | + } |
| 48 | + |
| 49 | + for(int i = 0, ncr, ncc; i < 4; ++i) { |
| 50 | + ncr = cr + dx[i]; |
| 51 | + ncc = cc + dy[i]; |
| 52 | + |
| 53 | + if(ncr < 0 || ncr >= r || ncc < 0 || ncc >= c || g[cl][ncr][ncc] == '#' || vis[cl][ncr][ncc]) |
| 54 | + continue; |
| 55 | + |
| 56 | + vis[cl][ncr][ncc] = true; |
| 57 | + q.push(make_pair(cl, make_pair(ncr, ncc))); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + ++cost; |
| 62 | + } |
| 63 | + |
| 64 | + return -1; |
| 65 | +} |
| 66 | + |
| 67 | +int main() { |
| 68 | + while(scanf("%d %d %d", &l, &r, &c) && l != 0 && r != 0 && c != 0) { |
| 69 | + for(int i = 0; i < l; ++i) |
| 70 | + for(int j = 0; j < r; ++j) |
| 71 | + for(int k = 0; k < c; ++k) { |
| 72 | + scanf(" %c", &g[i][j][k]); |
| 73 | + |
| 74 | + if(g[i][j][k] == 'S') |
| 75 | + sl = i, sr = j, sc = k; |
| 76 | + if(g[i][j][k] == 'E') |
| 77 | + el = i, er = j, ec = k; |
| 78 | + } |
| 79 | + |
| 80 | + int res = BFS(); |
| 81 | + |
| 82 | + if(res == -1) |
| 83 | + puts("Trapped!"); |
| 84 | + else |
| 85 | + printf("Escaped in %d minute(s).\n", res); |
| 86 | + } |
| 87 | + |
| 88 | + return 0; |
| 89 | +} |
0 commit comments