搜索POJ2251:Dungeon Master

Posted 听风不成泣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搜索POJ2251: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 <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 char dt[35][35][35];
 9 int via[35][35][35];
10 int k,n,m,sx,sy,sz,ex,ey,ez;
11 int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
12 
13 struct data
14 {
15     int x,y,z,step;
16 };
17 
18 int check(int x,int y,int z)
19 {
20     if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)
21         return 1;
22     else if(dt[x][y][z] == #)
23         return 1;
24     else if(via[x][y][z])
25         return 1;
26     return 0;
27 }
28 
29 int bfs()
30 {
31     int i;
32     data now,next;
33     queue<data> Q;
34     now.x = sx,now.y = sy,now.z = sz;
35     now.step = 0;
36     via[sx][sy][sz] = 1;
37     Q.push(now);
38     while(!Q.empty())
39     {
40         now = Q.front();
41         Q.pop();
42         if(now.x == ex && now.y == ey && now.z == ez)
43             return now.step;
44         for(i = 0; i<6; i++)
45         {
46             next = now;
47             next.x =now.x+to[i][0];
48             next.y = now.y+to[i][1];
49             next.z = now.z+to[i][2];
50             if(check(next.x,next.y,next.z))
51                 continue;
52             via[next.x][next.y][next.z] = 1;
53             next.step = now.step+1;
54             Q.push(next);
55         }
56     }
57     return 0;
58 }
59 
60 int main()
61 {
62     int i,j,r;
63     while(scanf("%d%d%d",&k,&n,&m))
64     {
65         if(k==0&&n==0&&m==0)
66         {
67             break;
68         }
69         for(i = 0; i<k; i++)
70         {
71             for(j = 0; j<n; j++)
72             {
73                 scanf("%s",dt[i][j]);
74                 for(r = 0; r<m; r++)
75                 {
76                     if(dt[i][j][r] == S)
77                     {
78                         sx = i,sy = j,sz = r;
79                     }
80                     else if(dt[i][j][r] == E)
81                     {
82                         ex = i,ey = j,ez = r;
83                     }
84                 }
85             }
86         }
87         memset(via,0,sizeof(via));
88         int ans;
89         ans = bfs();
90         if(ans)
91             printf("Escaped in %d minute(s).\n",ans);
92         else
93             cout << "Trapped!" <<endl;
94     }
95 
96     return 0;
97 }
View Code

 

以上是关于搜索POJ2251:Dungeon Master的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2251 Dungeon Master (BFS)

POJ2251Dungeon Master

Dungeon Master POJ-2251 三维BFS

POJ 2251 Dungeon Master

POJ 2251 Dungeon Master

POJ 2251 Dungeon Master (BFS)