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?
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.
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
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 0
Escaped in 11 minute(s). Trapped!
定义一个队列;
起始点入队;
while(队列不空){
队头结点出队;
若它是所求的目标状态,跳出循环;
否则,将它扩展出的子结点,全都入队;
}
若循环中找到目标,输出结果;
否则输出无解;
它的主要特点是:
n 每次队头元素出队时,扩展其全部的子结点,并用队列记录下来。
n 搜索过程没有回溯,是一种牺牲空间换取时间的方法。
代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int maxn=35;
int l,r,c;
int vis[maxn][maxn][maxn];
char ma[maxn][maxn][maxn];
int s[6][3]= {{1,0,0},{0,1,0},{0,0,1},{0,0,-1},{0,-1,0},{-1,0,0}};
struct node
{
int ll;//第几个
int rr;//行
int cc;//列
int step;
} p[maxn*maxn*maxn];
int bfs(int sx,int sy,int sz,int ex,int ey,int ez)
{
queue<node >que;
node fr,ne;
fr.ll=sx;
fr.rr=sy;
fr.cc=sz;
fr.step=0;
vis[fr.ll][fr.rr][fr.cc]=1;
que.push(fr); //第一个入队
while(!que.empty())
{
fr=que.front();
que.pop();//队头节点出队
if(fr.ll==ex&&fr.rr==ey&&fr.cc==ez)
return fr.step;
for(int i=0; i<6; i++)
{
ne.ll=fr.ll+s[i][0];
ne.rr=fr.rr+s[i][1];
ne.cc=fr.cc+s[i][2];
if(vis[ne.ll][ne.rr][ne.cc]==0&&ne.ll>=0&&ne.ll<l&&ne.cc>=0&&ne.cc<c&&ne.rr>=0&&ne.rr<r&&ma[ne.ll][ne.rr][ne.cc]!='#')
{
vis[ne.ll][ne.rr][ne.cc]=1;
ne.step=fr.step+1;
que.push(ne);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&c))
{
if(l==0&&r==0&&c==0) break;
memset(vis,0,sizeof(vis));
memset(ma,'\0',sizeof(ma));
int st1,st2,st3,en1,en2,en3;
for(int i=0; i<l; i++)
{
for(int j=0; j<r; j++)
{
scanf("%s",ma[i][j]);
for(int w=0; w<c; w++)
{
if(ma[i][j][w]=='S')
{
st1=i;
st2=j;
st3=w;
}
if(ma[i][j][w]=='E')
{
en1=i;
en2=j;
en3=w;
}
}
}
}
int ans=bfs(st1,st2,st3,en1,en2,en3);
if(ans==-1) printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
}