POJ 2251 Dungeon Master(bfs)

Dungeon Master

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!

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5
 6 struct point
 7 {
 8     int x,y,z;
 9     int step;
10 };
11
12 int sx,sy,sz,ex,ey,ez;
13 int L,R,C;
14 char map[32][32][32];
15 int vis[32][32][32];
16 int d[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,0,1},{0,-1,0},{0,0,-1}};
17
18 bool check(point node)
19 {
20     if(node.x==ex&&node.y==ey&&node.z==ez)
21     return true;
22     return false;
23 }
24
25 int bfs(int z,int x,int y)
26 {
27     queue<point>q;
28     point init;
29     init.x=x,init.y=y,init.z=z;
30     init.step=0;
31     q.push(init);
32     vis[z][x][y]=1;
33     while(!q.empty())
34     {
35         point t1,t2;
36         t1=q.front();
37         q.pop();
38         if(check(t1))
39         return t1.step;
40         for(int i=0;i<6;i++)
41         {
42             t2.z=t1.z+d[i][0];
43             t2.x=t1.x+d[i][1];
44             t2.y=t1.y+d[i][2];
45             t2.step=t1.step+1;
46             if(!vis[t2.z][t2.x][t2.y]&&map[t2.z][t2.x][t2.y]!=‘#‘)
47             {
48                 q.push(t2);
49                 vis[t2.z][t2.x][t2.y]=1;
50             }
51         }
52     }
53     return -1;
54 }
55
56 int main()
57 {
58     //freopen("in.txt","r",stdin);
59     char temp;
60     int i,j,k;
61     while(scanf("%d%d%d",&L,&R,&C),L||R||C)
62     {
63         memset(map,‘#‘,sizeof(map));
64         memset(vis,0,sizeof(vis));
65         for(i=1;i<=L;i++)
66         {
67             for(j=1;j<=R;j++)
68             {
69                 getchar();
70                 for(k=1;k<=C;k++)
71                 {
72                     scanf("%c",&temp);
73                     map[i][j][k]=temp;
74                     if(temp==‘S‘)
75                     sz=i,sx=j,sy=k;
76                     else if(temp==‘E‘)
77                     ez=i,ex=j,ey=k;
78                     else;
79                 }
80             }
81             getchar();
82         }
83         int ans=bfs(sz,sx,sy);
84         if(ans==-1)
85         printf("Trapped!\n");
86         else
87         printf("Escaped in %d minute(s).\n",ans);
88     }
89     return 0;
90 }
时间: 2024-10-03 14:14:56

POJ 2251 Dungeon Master(bfs)的相关文章

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

BFS POJ 2251 Dungeon Master

题目传送门 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 = 0x3f3

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 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

poj 2251 Dungeon Master 3维bfs(水水)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21230   Accepted: 8261 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

POJ 2251:Dungeon Master【bfs】

Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 7 Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge