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!
思路:bfs可以解决,用三维数组或三个一维数组模拟方向,总共6个(前后左右上下)
代码如下:
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int dx[6]={-1,1,0,0,0,0};
int dy[6]={0,0,-1,1,0,0};
int dz[6]={0,0,0,0,1,-1};
int l,r,c,time;
char map[30][30][30];
struct node{
int x,y,z;
int num;
};
node vn,vm;
bool isok(node a){
if(a.x<0||a.x>=r||a.y<0||a.y>=c||a.z<0||a.z>=l||map[a.z][a.x][a.y]=='#')
return false;
return true;
}
bool bfs(node st){
map[st.z][st.x][st.y]='#';
queue<node> q;
vn.x=st.x;
vn.y=st.y;
vn.z=st.z;
vn.num=0;
q.push(vn);
while(!q.empty()){
vn=q.front();
q.pop();
vm.x=vn.x;
vm.y=vn.y;
vm.z=vn.z;
time=vn.num;
for(int i=0;i<6;i++){
vn.x=vm.x+dx[i];
vn.y=vm.y+dy[i];
vn.z=vm.z+dz[i];
if(!isok(vn))
continue;
if(map[vn.z][vn.x][vn.y]=='E'){
vn.num=time+1;
return true;
}
vn.num=time+1;
q.push(vn);
map[vn.z][vn.x][vn.y]='#';
}
}
return false;
}
int main(){
while(~scanf("%d%d%d",&l,&r,&c)){
if(l==0&&r==0&&c==0)
break;
node st,ed;
for(int i=0;i<l;i++)
for(int j=0;j<r;j++){
scanf("%s",&map[i][j]);
for(int k=0;k<c;k++)
if(map[i][j][k]=='S'){
st.z=i;
st.x=j;
st.y=k;
}
}
if(bfs(st))
printf("Escaped in %d minute(s).\n",vn.num);
else
printf("Trapped!\n");
}
return 0;
}