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
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.
Output
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Sample Input
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!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output
Escaped in 11 minute(s). Trapped!
就是多了一个维度其他没有啥的
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
char maze[33][33][33];
int book[33][33][33];
int n,m,k,bx,by,bz,ex,ey,ez;
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct node
{
int x,y,z,step;
};
bool check(int x,int y,int z){
if(x<0||y<0||z<0||x>=n||y>=m||z>=k)return false;
if(maze[x][y][z]=='#')return false;
if(book[x][y][z]==1)return false;
return true;
}
int BFS()
{
queue<node>qu;
node beginn={bx,by,bz,0};
qu.push(beginn);
book[bx][by][bz]=1;
while(qu.empty()==0){
node tem=qu.front();
qu.pop();
if(tem.x==ex&&tem.y==ey&&tem.z==ez)
{
printf("Escaped in %d minute(s).\n",tem.step);
return 1;
}
for(int i=0;i<6;i++){
int fx=tem.x+dir[i][0];
int fy=tem.y+dir[i][1];
int fz=tem.z+dir[i][2];
if(check(fx,fy,fz)){
node endd;
endd.x=fx;
endd.y=fy;
endd.z=fz;
endd.step=tem.step+1;
qu.push(endd);
book[fx][fy][fz]=1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k))
{
if(!n&&!m&&!k)break;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%s",maze[i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int x=0;x<k;x++)
{
if(maze[i][j][x]=='S'){
bx=i;by=j;bz=x;
}else if(maze[i][j][x]=='E'){
ex=i;ey=j;ez=x;
}
}
}
}
memset(book,0,sizeof(book));
if(!BFS())printf("Trapped!\n");
}
return 0;
}