poj 2251 Dungeon Master

时间:2023-03-08 15:48:42
poj 2251 Dungeon Master

http://poj.org/problem?id=2251

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18773   Accepted: 7285

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! 分析:
典型的三维广搜。 AC代码:
 #include<cstdio>
#include<cstring>
#include<queue>
using namespace std; #define INF 0x3f3f3f3f char maz[][][];
int d[][][];
int sx,sy,sz;
int ex,ey,ez;
int dx[] = {,-,,,,};
int dy[] = {,,,-,,};
int dz[] = {,,,,,-};
int X,Y,Z;
struct P{
int x,y,z;
P(int xx,int yy,int zz) :x(xx),y(yy),z(zz) {
}
}; int bfs() {
memset(d,INF,sizeof(d));
queue<P> que;
que.push(P(sx,sy,sz));
d[sx][sy][sz] = ; while(que.size()) {
P p = que.front();que.pop(); if(p.x == ex && p.y == ey && p.z == ez) break; for(int i = ;i < ;i++) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
int nz = p.z + dz[i]; if( <= nx && nx < X && <= ny && ny < Y && <= nz && nz < Z
&& maz[nx][ny][nz] != '#' && d[nx][ny][nz] == INF) {
que.push(P(nx,ny,nz));
d[nx][ny][nz] = d[p.x][p.y][p.z] + ;
}
}
}
if(d[ex][ey][ez] == INF) return -;
return d[ex][ey][ez];
} int main() {
while(~scanf("%d %d %d",&X,&Y,&Z)) {
if(X == && Y == && Z == ) break;
for(int i = ;i < X;i++) {
for(int j = ;j < Y;j++) {
scanf("%s",maz[i][j]);
for(int k = ;k < Z;k++) {
if(maz[i][j][k] == 'S') {
sx = i;
sy = j;
sz = k;
} else if(maz[i][j][k] == 'E') {
ex = i;
ey = j;
ez = k;
}
}
}
} int res = bfs();
if(res == -) {
printf("Trapped!\n");
} else {
printf("Escaped in %d minute(s).\n",res);
}
}
return ;
}