POJ 2251 题解

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27520   Accepted: 10776

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 "iostream"
 2 #include "cstdio"
 3 #include "fstream"
 4 #include "sstream"
 5 #include "cstring"
 6
 7 using namespace std;
 8 const int maxN = 3e4 + 1e2 ;
 9 struct Queue { int x , y , z ; } ;
10 typedef long long QAQ ;
11
12 int dx[ ] = { 1 , -1 , 0 , 0 , 0 , 0 } ;
13 int dy[ ] = { 0 , 0 , 0 , 0 , -1 , 1 } ;
14 int dz[ ] = { 0 , 0 , -1 , 1 , 0 , 0 } ;
15
16 int step[ maxN ] ;
17 Queue Q[ maxN ] ;
18 bool vis[ 40 ][ 40 ][ 40 ] ;
19 char map[ 40 ][ 40 ][ 40 ] ;
20
21 int L , R , C ;
22 int start_x , start_y , start_z , des_x , des_y , des_z ;
23 //QAQ Ans ;
24
25 void Init ( ) {
26         memset ( vis , false , sizeof ( vis ) ) ;
27     memset ( step , 0 , sizeof ( step ) ) ;
28 }
29
30 inline bool Check ( const int x_x , const int y_y , const int z_z ) {return ( x_x == des_x && y_y == des_y && z_z == des_z ) ? true : false ; }
31 int BFS ( ) {
32     int Head = 0 , Tail = 0 ;
33     Q[ Tail ].x = start_x ;
34         Q[ Tail ].y = start_y ;
35         Q[ Tail ].z = start_z ;
36     while ( Head <= Tail ) {
37         for ( int i=0 ; i<6 ; ++i ) {
38             int xx = Q[ Head ].x + dx[ i ] ;
39             int yy = Q[ Head ].y + dy[ i ] ;
40             int zz = Q[ Head ].z + dz[ i ] ;
41             if( !vis[ xx ][ yy ][ zz ] && ( map[ xx ][ yy ][ zz ] == ‘.‘  ) && xx >= 0 && xx < L && yy >= 0 && yy < R && zz >= 0 && zz < C ) {
42                 vis[ xx ][ yy ][ zz ] = true ;
43                 Q[ ++Tail ].x = xx ;
44                 Q[ Tail ].y = yy ;
45                 Q[ Tail ].z = zz ;
46                 step [ Tail ] = step[ Head ] + 1 ;
47                 if ( Check ( xx , yy , zz ) ) return step[ Tail ] ;
48             }
49         }
50         ++ Head ;
51     }
52     return false ;
53 }
54
55 void Scan ( ) {
56         for ( int i=0 ; i<L ; ++i , getchar ( ) ){
57         for ( int j=0 ; j<R ; ++j , getchar ( ) ){
58                 for ( int k=0 ; k<C ; ++k ) {
59                 map[ i ][ j ][ k ] = getchar ( ) ;
60                 if ( map[ i ][ j ][ k ] == ‘S‘ ) {
61                     start_x = i ;
62                                         start_y = j ;
63                                         start_z = k ;
64                                 }
65                                 else if ( map[ i ][ j ][ k ] == ‘E‘ ) {
66                                         map[ i ][ j ][ k ] = ‘.‘ ;
67                                         des_x = i ;
68                                         des_y = j ;
69                                         des_z = k ;
70                                 }
71             }
72                 }
73         }
74 }
75
76 inline void Print ( int temp ) {
77         if( temp ) printf ( "Escaped in %d minute(s).\n" , temp ) ;
78     else printf ( "Trapped!\n" ) ;
79 }
80
81 int main ( ) {
82     while ( scanf ( "%d %d %d\n " , &L , &R , &C ) == 3 && L && R && C ) {
83             Init ( ) ;
84             Scan ( );
85             Print ( BFS( ) ) ;
86     }
87     return 0 ;
88 }

2016-10-19 18:50:40

(完)

时间: 2024-10-24 11:23:38

POJ 2251 题解的相关文章

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(用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

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

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 三维地图bfs

三维地图 poj 2251 #include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; char map[35][35][35]; int l,r,c; bool book[35][35][35]; // 定义 东西南北和上下 struct dis{ int x,y,z; int step; dis(int x,int y,int z,i

poj 2251 Dungeon Master 题解

非常基本的BFS模板题,忘记了BFS模板的话就回来看看吧. 大致题意就是是一个三维的数组然后从入口找出口嘛! #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; char map[31][31][31]; int c,l,r; int is,js,ks; int d[31][31][31] ;//到起点的距离的数组 bfs的作用本

POJ - 2251 Dungeon Master(三维BFS)

题目链接:http://poj.org/problem?id=2251 题意:三维BFS. 题解:大水题,只不过多加了两个方向 1 //poj2251 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int sx,sy,sz,ex,ey,ez,L,R,C; 8 const int INF=

poj 2251(广搜求两点之间的距离)

题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16682   Accepted: 6491 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit