【题解】POJ-2251 Dungeon Master

Dungeon Master (POJ - 2251)

题面

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?

输入

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.

输出

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!            

样例输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

样例输出

1
2
Escaped in 11 minute(s).
Trapped!

提示

思路

六个方向,bfs即可

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 30+5;

char b[N][N][N] = {{{0}}};
bool vis[N][N][N] = {{{0}}};
int l, r, c, ans = 0;

int dx[] = { 1, -1, 0, 0, 0, 0};
int dy[] = { 0, 0, 1, -1, 0, 0};
int dz[] = { 0, 0, 0, 0, 1, -1};

struct P {
int z, x, y;
int step;
};
P sp;

bool bfs() {
memset(vis, 0, sizeof(vis));

sp.step = 0;

queue<P> q;
q.push(sp);
vis[sp.z][sp.x][sp.y] = 1;

while(!q.empty()) {
P tp = q.front();
q.pop();

P np = tp;

for(int i=0; i<6; i++) {
int z = np.z = tp.z+dz[i];
int x = np.x = tp.x+dx[i];
int y = np.y = tp.y+dy[i];
np.step = tp.step+1;

if(z<0 || z>=l || x<0 || x>=r || y<0 || y>=c) {
continue;
}
if(b[z][x][y]=='#') {
continue;
}
if(b[z][x][y]=='E') {
ans = np.step;
return true;
}
if(b[z][x][y]=='.' && vis[z][x][y]==false) {
vis[z][x][y] = true;
q.push(np);
}
}
}
return false;
}

int main(void) {
while(scanf("%d%d%d", &l, &r, &c)==3 && l && r && c) {
ans = 0;

for(int i=0; i<l; i++) {
for(int j=0; j<r; j++) {
for(int k=0; k<c; k++) {
scanf(" %c", &b[i][j][k]);
if(b[i][j][k]=='S') {
sp.z = i;
sp.x = j;
sp.y = k;
}
}
}
}
if(bfs())
printf("Escaped in %d minute(s).\n", ans);
else
printf("Trapped!\n");
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/