B - Dungeon Master POJ - 2251

B - Dungeon Master

POJ - 2251

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
 1 #include<set>
 2 #include<map>
 3 #include<cmath>
 4 #include<queue>
 5 #include<string>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<sstream>
 9 #include<cstdlib>
10 #include<cstring>
11 #include<sstream>
12 #include<iostream>
13 #include<algorithm>
14 
15 using namespace std;
16 const int maxn = 40;
17 const int INF = 0x3f3f3f3f;
18 const int mod = 7;
19 typedef long long ll;
20 #define PI 3.1415927
21 
22 char mp[maxn][maxn][maxn];
23 int mv[6][3] = {-1,0,0,0,-1,0,0,0,-1,1,0,0,0,1,0,0,0,1};
24 int sx,sy,sz,gx,gy,gz;
25 int l, r, c;
26 int ans = 0;
27 bool flag;
28 bool vis[maxn][maxn][maxn];
29 struct Node
30 {
31     int x, y, z;
32     int step;
33     Node(int _x, int _y, int _z, int _s){x=_x;y=_y;z=_z;step=_s;}
34 };
35 void bfs()
36 {
37     queue<Node> que;
38     que.push(Node(sx,sy,sz,0));
39     vis[sx][sy][sz] = true;
40     while(!que.empty())
41     {
42         Node nn = que.front();
43         que.pop();
44         for(int i = 0; i < 6; ++i)
45         {
46             int nx = nn.x+mv[i][0]; int ny = nn.y+mv[i][1]; int nz = nn.z+mv[i][2];
47             if(0<=nx&&nx<l&&0<=ny&&ny<r&&0<=nz&&nz<c&&!vis[nx][ny][nz]&&mp[nx][ny][nz]!='#')
48             {
49                 if(mp[nx][ny][nz] == 'E')
50                 {
51                     flag = true;
52                     ans = nn.step+1;
53                     break;
54                 }
55                 else
56                 {
57                     vis[nx][ny][nz] = true;
58                     que.push(Node(nx,ny,nz,nn.step+1));
59                 }
60             }
61         }
62     }
63 }
64 int main()
65 {
66     while(~scanf("%d%d%d", &l, &r, &c) && (l||r||c))
67     {
68         flag = false;
69         getchar();
70         ans = 0;
71         memset(vis, 0, sizeof(vis));
72         for(int i = 0; i < l; ++i)
73         {
74             for(int j = 0; j < r; ++j)
75             {
76                 for(int k = 0; k < c; ++k)
77                 {
78                     mp[i][j][k] = getchar();
79                     if(mp[i][j][k] == 'S')
80                     {
81                         sx=i;sy=j;sz=k;
82                     }
83                     if(mp[i][j][k] == 'E')
84                     {
85                         gx=i;gy=j;gz=k;
86                     }
87                 }
88                 getchar();
89             }
90             getchar();
91         }
92         bfs();
93         if(flag)
94             printf("Escaped in %d minute(s).\n", ans);
95         else
96             printf("Trapped!\n");
97     }
98     return 0;
99 }
AC代码

 

posted @ 2018-07-31 14:55  focus5679  阅读(121)  评论(0编辑  收藏  举报