1. 程式人生 > >POJ 2251 Dungeon Master (三維BFS)

POJ 2251 Dungeon Master (三維BFS)

題目連結: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程式碼