POJ 2251-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!

开三维数组用BFS求。
 1 #include<cstdio>
 2 #include<queue>
 3 using namespace std;
 4 int dx[6]={-1,1,0,0,0,0};
 5 int dy[6]={0,0,-1,1,0,0};
 6 int dz[6]={0,0,0,0,1,-1};
 7 char str[100][100][100];
 8 int l,r,c,i,j,k,ans,bx,bz,by;
 9 struct stu
10 {
11     int x,y,z,step;
12 };
13 bool f(stu st)
14 {
15     if(st.x<0 || st.z<0 || st.y<0 || st.x>=r || st.y>=c || st.z>=l || str[st.z][st.x][st.y] ==‘#‘)
16     {
17         return false;
18     }
19     return true;
20 }
21 int bfs(int bz,int bx,int by)
22 {
23     str[bz][bx][by]=‘#‘;
24     int nx,ny,time,zz,xx,yy;
25     queue<stu> que;
26     stu st,next;
27     st.x=bx;
28     st.y=by;
29     st.z=bz;
30     st.step=0;
31     que.push(st);
32
33     while(!que.empty())
34     {
35
36         st=que.front();
37         que.pop();
38
39         xx=st.x;
40         yy=st.y;
41         zz=st.z;
42         time=st.step;
43         for(i = 0 ;i < 6 ; i++)
44         {
45             st.x=xx+dx[i];
46             st.y=yy+dy[i];
47             st.z=zz+dz[i];
48             if(!f(st))
49             {
50                 continue;
51             }
52             if(str[st.z][st.x][st.y] == ‘E‘)
53             {
54                 return time+1;
55             }
56
57                 st.step=time+1;
58                 que.push(st);
59                 str[st.z][st.x][st.y]=‘#‘;
60         }
61     }
62     return 0;
63 }
64 int main()
65 {
66 while(scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)
67 {
68     for(i = 0 ; i < l ; i++)
69     {
70         for(j = 0 ; j < r ;j++)
71         {
72             scanf("%s",str[i][j]);
73             for(k = 0 ; k < c ; k++)
74             {
75                 if(str[i][j][k] == ‘S‘)
76                 {
77                     bz=i;
78                      bx=j;
79                      by=k;
80                 }
81             }
82         }
83     }
84     ans=bfs(bz,bx,by);
85     if(ans)
86         printf("Escaped in %d minute(s).\n",ans);
87     else
88         printf("Trapped!\n");
89
90 }
91 }
时间: 2024-10-12 18:52:25

POJ 2251-Dungeon Master (三维空间求最短路径)的相关文章

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(地牢大师)

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(三维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(三维空间bfs)

题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXN = 30 + 10; char pic[MAXN][MAXN][MAXN]; bool vis[MAXN][MAXN][MAXN]; int sx, sy, sz; int dr[] = {0, 0, 1, -1, 0, 0};

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: 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(优先队列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 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 dow

POJ 2251 Dungeon Master (广搜)

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