POJ2251 Dungeon Master —— BFS

题目链接:http://poj.org/problem?id=2251


 

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37296   Accepted: 14266

 

Description

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!

Source

 


题解:

三维的纯BFS。

 

 

代码如下:

 1 #include <iostream>  
 2 #include <cstdio>  
 3 #include <cstring>  
 4 #include <cmath>  
 5 #include <algorithm>  
 6 #include <vector>  
 7 #include <queue>  
 8 #include <stack>  
 9 #include <map>  
10 #include <string>  
11 #include <set>  
12 #define ms(a,b) memset((a),(b),sizeof((a)))  
13 using namespace std;  
14 typedef long long LL;  
15 const int INF = 2e9;  
16 const LL LNF = 9e18;  
17 const int MOD = 1e9+7;  
18 const int MAXN = 30+10;  
19   
20 struct node  
21 {  
22     int x, y, z, step;  
23 };  
24   
25 int L, R, C;  
26 char m[MAXN][MAXN][MAXN];  
27 int vis[MAXN][MAXN][MAXN];  
28 int dir[6][3] = {  {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };  
29   
30 queue<node>que;  
31 int bfs(node s, node e)  
32 {  
33     ms(vis,0);  
34     while(!que.empty()) que.pop();  
35   
36     s.step = 0;  
37     vis[s.x][s.y][s.z] = 1;  
38     que.push(s);  
39   
40     while(!que.empty())  
41     {  
42         node now = que.front();  
43         que.pop();  
44   
45         if(now.x==e.x && now.y==e.y && now.z==e.z)  
46             return now.step;  
47   
48         for(int i = 0; i<6; i++)  
49         {  
50             node tmp;  
51             tmp.x = now.x + dir[i][0];  
52             tmp.y = now.y + dir[i][1];  
53             tmp.z = now.z + dir[i][2];  
54             if(tmp.x>=1 && tmp.x<=L && tmp.y>=1 && tmp.y<=R && tmp.z>=1 && tmp.z<=C    
55                 && m[tmp.x][tmp.y][tmp.z]!='#' && !vis[tmp.x][tmp.y][tmp.z] )  
56             {  
57                 vis[tmp.x][tmp.y][tmp.z] = 1;  
58                 tmp.step = now.step + 1;  
59                 que.push(tmp);  
60             }  
61         }  
62     }  
63     return -1;  
64 }  
65   
66 int main()  
67 {  
68     while(scanf("%d%d%d",&L, &R, &C) && (L||R||C))  
69     {  
70         for(int i = 1; i<=L; i++)  
71         for(int j = 1; j<=R; j++)  
72             scanf("%s", m[i][j]+1);  
73   
74         node s, e;  
75         for(int i = 1; i<=L; i++)  
76         for(int j = 1; j<=R; j++)  
77         for(int k = 1; k<=C; k++)  
78         {  
79             if(m[i][j][k]=='S') s.x = i, s.y = j, s.z = k;  
80             if(m[i][j][k]=='E') e.x = i, e.y = j, e.z = k;  
81         }  
82   
83         int ans = bfs(s,e);  
84         if(ans==-1)  
85             printf("Trapped!\n");  
86         else  
87             printf("Escaped in %d minute(s).\n", ans);  
88     }  
89     return 0;  
90 }  
View Code

 

posted on 2017-09-01 11:17  h_z_cong  阅读(184)  评论(0编辑  收藏  举报

导航