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
Ulm Local 1997
题目大意:一个三维的迷宫,要从S走到E。如果是“.”就能走。每走一步的时间是1分钟,给出最短时间。
解题思路:说来惭愧,这个题我当时脑子没转过弯来,还是看了别人的题解才搞清楚怎么做。就是一个bfs。只不过要搜索的方向是六个而已。
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 35;
char map[MAXN][MAXN][MAXN];
int endx,endy,endz;
int xx,yy,zz;
int next[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int flag[MAXN][MAXN][MAXN];
struct node
{
int x,y,z;
int step;
};
void putQueue(node a);
void init()
{
for(int i=0; i<MAXN; i++){
for(int j=0; j<MAXN; j++)
for(int k=0; k<MAXN; k++)
flag[i][j][k] = 0;
}
}
bool check(node a)
{
if(a.x>=0 && a.x<xx && a.y>=0 && a.y<yy && a.z>=0 && a.z<zz && map[a.z][a.x][a.y]!='#')
if(flag[a.z][a.x][a.y] == 0){
flag[a.z][a.x][a.y] = 1;
return true;
}
return false;
}
int serch(int bx,int by,int bz)
{
queue<node> Q;
node a,b;
a.x = bx; a.y = by; a.z = bz;a.step=0;
Q.push(a);
flag[bz][bx][by] = 1;
while(!Q.empty()){
a = Q.front();
Q.pop();
if(a.x==endx&&a.y==endy&&a.z==endz ) {
return a.step;
}
for(int i=0; i < 6; i++){
b.x = a.x + next[i][0];
b.y = a.y + next[i][1];
b.z = a.z + next[i][2];
b.step = a.step+1;
if(check(b)){
Q.push(b);
}
}
}
return -1;
}
int main()
{
void input();
while(~scanf("%d %d %d",&zz,&xx,&yy)&&xx+yy+zz){
init();
int beginx,beginy,beginz;
int x,y,z;
for(int z=0; z<zz; z++){ //i == x
for(int x=0; x<xx; x++){ //j == y
scanf("%s",&map[z][x]);
for(int y=0; y<yy;y++){ //x == z
if(map[z][x][y] == 'S'){
beginx = x;
beginy = y;
beginz = z;
}
if(map[z][x][y] == 'E'){
endx = x;
endy = y;
endz = z;
}
}
}
}
int a = serch(beginx,beginy,beginz);
if(a != -1)
printf("Escaped in %d minute(s).\n",a);
else
printf("Trapped!\n");
}
return 0;
}