题目传送门

 1 /*
 2     BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴
 3 */
 4 #include <cstdio>
 5 #include <algorithm>
 6 #include <cstring>
 7 #include <queue>
 8 using namespace std;
 9 
10 const int MAXN = 33;
11 const int INF = 0x3f3f3f3f;
12 struct Point    {
13     int x, y, z, step;
14 };
15 char maze[MAXN][MAXN][MAXN];
16 int dx[6] = {-1, 1, 0, 0, 0, 0};
17 int dy[6] = {0, 0, -1, 1, 0, 0};
18 int dz[6] = {0, 0, 0, 0, -1, 1};
19 bool vis[MAXN][MAXN][MAXN];
20 int l, r, c;
21 
22 bool judge(int x, int y, int z) {
23     if (x < 1 || x > r || y < 1 || y > c || z < 1 || z > l || vis[z][x][y] || maze[z][x][y] == '#') return false;
24     return true;
25 }
26 
27 void BFS(void)  {
28     int sx, sy, sz, ex, ey, ez;
29     for (int i=1; i<=l; ++i)    {
30         for (int j=1; j<=r; ++j)    {
31             for (int k=1; k<=c; ++k)    {
32                 if (maze[i][j][k] == 'S')   {
33                     sx = j; sy = k; sz = i;
34                 }
35                 else if (maze[i][j][k] == 'E')  {
36                     ex = j; ey = k; ez = i;
37                 }
38             }
39         }
40     }
41     memset (vis, false, sizeof (vis));
42     queue<Point> Q; Q.push ((Point) {sx, sy, sz, 0});
43     bool flag = false;  vis[sz][sx][sy] = true;
44     while (!Q.empty ()) {
45         Point p = Q.front ();   Q.pop ();
46         if (p.x == ex && p.y == ey && p.z == ez)  {
47             printf ("Escaped in %d minute(s).\n", p.step);
48             flag = true;    break;
49         }
50         for (int i=0; i<6; ++i) {
51             int tx = p.x + dx[i];   int ty = p.y + dy[i];   int tz = p.z + dz[i];
52             if (judge (tx, ty, tz)) {
53                 vis[tz][tx][ty] = true;
54                 Q.push (Point {tx, ty, tz, p.step + 1});
55             }
56         }
57     }
58     if (!flag)  puts ("Trapped!");
59 }
60 
61 int main(void)  {       //POJ 2251 Dungeon Master
62     while (scanf ("%d%d%d", &l, &r, &c) == 3)   {
63         if (!l && !r && !c) break;
64         for (int i=1; i<=l; ++i)    {
65             for (int j=1; j<=r; ++j)    scanf ("%s", maze[i][j] + 1);
66         }
67         BFS ();
68     }
69 
70     return 0;
71 }

 

编译人生,运行世界!