Fellow me on GitHub

POJ2251(KB1-B 三维BFS)

Dungeon Master

Time Limit: 1000MS  Memory Limit: 10000K

Total Submissions: 40872  Accepted: 19936

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!

Source

 
三维的BFS。
 1 //2017-02-20
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 
 7 using namespace std;
 8 
 9 int dx[6] = {0, 0, 1, 0, -1, 0};
10 int dy[6] = {0, 1, 0, -1, 0, 0};
11 int dz[6] = {1, 0, 0, 0, 0, -1};
12 
13 struct node
14 {
15     int x, y, z, step;
16 };
17 
18 int main()
19 {
20     int level, n, m;
21     char dun[35][35][35];
22     bool vis[35][35][35];
23     node tmp;
24     while(cin>>level>>n>>m)
25     {
26         if(level == 0 && n == 0 && m == 0)break;
27         queue<node> q;
28         memset(vis, 0, sizeof(vis));
29         for(int k = 0; k < level; k++)
30               for(int i = 0; i < n; i++)
31                   for(int j = 0; j < m; j++){
32                       cin>>dun[i][j][k];
33                     if(dun[i][j][k] == 'S'){
34                         tmp.x = i;
35                         tmp.y = j;
36                         tmp.z = k;
37                         tmp.step = 0;
38                         q.push(tmp);
39                         vis[i][j][k] = 1;
40                     }
41                 }
42         int x, y, z, step, nx, ny, nz;
43         bool ok = false;
44         while(!q.empty())
45         {
46             x = q.front().x;
47             y = q.front().y;
48             z = q.front().z;
49             step = q.front().step;
50             for(int i = 0; i < 6; i++)
51             {
52                 nx = x+dx[i];
53                 ny = y+dy[i];
54                 nz = z+dz[i];
55                 if(nx>=0&&nx<n&&ny>=0&&ny<m&&nz>=0&&nz<level&&!vis[nx][ny][nz])
56                 {
57                     if(dun[nx][ny][nz] == '.'){
58                         vis[nx][ny][nz] = 1;
59                         tmp.x = nx;
60                         tmp.y = ny;
61                         tmp.z = nz;
62                         tmp.step = step+1;
63                         q.push(tmp);
64                     }else if(dun[nx][ny][nz] == 'E'){
65                         ok = true;
66                         cout<<"Escaped in "<<step+1<<" minute(s)."<<endl;
67                         break;
68                     }
69                 }
70             }
71             if(ok)break;
72             q.pop();
73         }
74         if(!ok)cout<<"Trapped!"<<endl;
75     }
76 
77     return 0;
78 }

 

posted @ 2017-02-20 21:18  Penn000  阅读(179)  评论(0编辑  收藏  举报