Submit
Status
Practice
POJ 2251
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!
代码
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
char c[50][50][50];
int vis[50][50][50]; //对访问过的点进行标记
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int i, k, j, x, y, n, m, sx, sy,sz; //用sx,sy,sz来表示起点S的三维坐标
struct T { //x,y,z表示任意点的坐标,cnt表示到达此点时的步数
int z;
int x;
int y;
int cnt;
}now, eed;
int can(struct T a) {//约束条件
if(c[a.z][a.x][a.y] == '#' || a.z <= 0 || a.z > k || a.x <= 0 || a.x > n || a.y <= 0 || a.y > m || vis[a.z][a.x][a.y])
return 0;
return 1;
}
int bfs()//广度优先搜索
{
queue<T> maze;
memset(vis, 0, sizeof(vis));
now.z = sz; now.x = sx; now.y = sy; now.cnt = 0;
maze.push(now); vis[now.z][now.x][now.y] = 1;
while( !maze.empty())
{
now = maze.front();
if(c[now.z][now.x][now.y] == 'E') return now.cnt; //找到终点,返回终点的cnt值
eed.cnt = now.cnt + 1;
for(i = 0; i < 4; i++) { //二维平面内向四个方向搜索, 即north, south, east, west
eed.z = now.z;
eed.x = now.x + dir[i][0];
eed.y = now.y + dir[i][1];
if(can(eed))//如果符合条件,就放到队列中
{
maze.push(eed);
vis[eed.z][eed.x][eed.y] = 1;
}
}
//二维平面的上方,即up
eed.z = now.z + 1;
eed.x = now.x;
eed.y = now.y;
if(can(eed))
{
maze.push(eed);
vis[eed.z][eed.x][eed.y] = 1;
}
//二维平面的下方,即down
eed.z = now.z - 1;
eed.x = now.x;
eed.y = now.y;
if(can(eed))
{
maze.push(eed);
vis[eed.z][eed.x][eed.y] = 1;
}
maze.pop();
}
return -1;
}
int main()
{
while(scanf("%d %d %d", &k, &n, &m) && k+n+m != 0) {
for(int i = 1; i <= k; i++) {
for(int j = 1; j <= n; j++) {
for(int a = 1; a <= m; a++) {
cin >> c[i][j][a];
if(c[i][j][a] == 'S')
{
sz = i;
sx = j;
sy = a;
}
}
}
}
int result = bfs();
if(result == -1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", result);
}
return 0;
}