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!
题意
三维地牢,#不能走,.能走,S起点,E终点,求最短路径
思路
最短路径,广搜,三维广搜问题
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
char str[33][33][33]; //地图
int l,r,c,i,j,k,bx,by,bz,ans;
int ne[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; //下一步坐标
struct stu //点及到达此点的最短步数
{
int x,y,z,step;
};
bool ok(stu st) //判断能否走
{
if(st.x<0 || st.z<0 || st.y<0 || st.y>=r || st.z>=c || st.x>=l || str[st.x][st.y][st.z] =='#')
return false;
return true;
}
int bfs(int x,int y,int z)
{
str[x][y][z] = '#';
int nx,ny,time,xx,yy,zz;
queue<stu> que; //队列
stu st,next;
st.x = x;
st.y = y;
st.z = z;
st.step = 0;
que.push(st);
while(!que.empty())
{
st=que.front();
que.pop();
xx = st.x;
yy = st.y;
zz = st.z;
time = st.step;
for(i=0;i<6;i++)
{
st.x = xx+ne[i][0];
st.y = yy+ne[i][1];
st.z = zz+ne[i][2];
if(!ok(st))
{
continue;
}
if(str[st.x][st.y][st.z]=='E') //到达终点
{
return time+1;
}
st.step = time+1;
que.push(st);
str[st.x][st.y][st.z] = '#';
}
}
return 0; //不能实现
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
{
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
cin>>str[i][j];
getchar();
}
flag = 0;
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
for(k=0;k<c;k++)
{
if(str[i][j][k]=='S')
{
ans=bfs(i,j,k); //起点
}
}
if(k!=c) break;
}
if(j!=r) break;
}
if(ans) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}