码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2251 Dungeon Master (三维BFS)

时间:2018-11-27 01:15:44      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:pre   follow   evel   lin   getchar   ios   where   art   code   

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

 

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51402   Accepted: 19261

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!

题意: 在一个三维空间中,# 表示石块,不能走,. 表示路,可以走,求从 S 到 E 花费的最短时间,每走一步花费时间加 1 。
注意:
1.不要写 if(bfs()) printf("%d",bfs()),这种形式,要写一个中间变量 int ans = bfs() ,然后,if(ans) printf("%d",ans);
2.这是一个三维空间,有六个方向
3.不要只判断当前位置是不是被标记过,还要判断当前位置是不是石头(“#”);

技术分享图片
 1 #include<iostream>
 2 #include<queue>
 3 #include <cstring>
 4 #include<cstdio>
 5 
 6 using namespace std;
 7 
 8 const int maxn = 35;
 9 int go[6][3] = {0,0,1,0,0,-1,-1,0,0,1,0,0,0,1,0,0,-1,0};
10 bool mark[maxn][maxn][maxn];
11 char maze[maxn][maxn][maxn];
12 int sx,sy,sz,ex,ey,ez;
13 int L,R,C;
14 struct node
15 {
16     int x,y,z;
17     int time;
18 };
19 
20 bool Isok(int x,int y,int z)
21 {
22     //别忘了判断是不是‘#‘
23     return x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C && !mark[x][y][z] && maze[x][y][z] != #;
24 }
25 int bfs()
26 {
27     queue<node> q;
28     struct node cu,ne;
29     cu.x=sx;cu.y=sy;cu.z=sz;
30     cu.time = 0;
31     mark[sx][sy][sz]=1;
32     q.push(cu);
33     while(!q.empty())
34     {
35         cu = q.front();
36         q.pop();
37         if(cu.x==ex&&cu.y==ey&&cu.z==ez)
38             return cu.time;
39         for(int i=0;i<6;i++)
40         {
41             ne.x = cu.x + go[i][0];
42             ne.y = cu.y + go[i][1];
43             ne.z = cu.z + go[i][2];
44             if(Isok(ne.x,ne.y,ne.z))
45             {
46                 mark[ne.x][ne.y][ne.z] = 1;
47                 ne.time = cu.time + 1;
48                 q.push(ne);
49             }
50         }
51     }
52     return 0;
53 }
54 int main()
55 {
56     while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C))
57     {
58         memset(mark,0,sizeof(mark));
59         memset(maze,0,sizeof(maze));
60         for(int i=0;i<L;i++)
61         {
62             for(int j=0;j<R;j++)
63                 scanf("%s",maze[i][j]);
64             getchar(); //有没有getchar()都能过
65         }
66         for(int i=0;i<L;i++)
67         {
68             for(int j=0;j<R;j++)
69             {
70                 for(int k=0;k<C;k++)
71                 {
72                     if(maze[i][j][k] == S)
73                     {
74                         sx=i;sy=j;sz=k;
75                     }
76                     else if(maze[i][j][k] == E)
77                     {
78                         ex=i;ey=j;ez=k;
79                     }
80                 }
81             }
82         }
83         //不要直接使用bfs()的返回值做判断和做printf输出变量。
84         int ans = bfs();
85         if(ans)
86             printf("Escaped in %d minute(s).\n",ans);
87         else
88             printf("Trapped!\n");
89     }
90     return 0;
91 }
Ac代码

 

POJ 2251 Dungeon Master (三维BFS)

标签:pre   follow   evel   lin   getchar   ios   where   art   code   

原文地址:https://www.cnblogs.com/youpeng/p/10023557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!