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?
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.
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!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
此题是简单BFS,刚开始我用DFS一直显示超时的,所以用BFS一般来寻找最短路径。
int dx[4]={0, 1, 0, -1};//右 下 左 上
int dy[4]={ 1, 0,-1, 0};
struct place{
int num,x,y;
int fa;
}pla[28000];
int head,tail;
这是上下左右方向的数组定义,但是在每遍历到一个新点的时候。
1.判断是否是出口。
2.判断它的下一层或者上一层能不能行走。
3.判断它的四个方向。
4.后来我将程序简化了一下,就省去了main函数里面,用while循环来找总共步数的,visited数组的值=该点的步数==父亲节点的步数+1,最后通过XY来直接在visited里面进行读取
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char map[33][33][33];
int visited[33][33][33];
int L,R,C;// 魔方格数量 行 列
int total;// 总共步数
int dx[4]={0, 1, 0, -1};//右 下 左 上
int dy[4]={ 1, 0,-1, 0};
int flag=0;
struct place{
int num,x,y;
int fa;
}pla[28000];
int head,tail;
void BFS(int num,int x,int y)
{
int x1,y1,num1;
int x2,y2,num2;
visited[num][x][y]=1;
pla[0].num=num,pla[0].x=x,pla[0].y=y;
head=0,tail=1;
while(head<tail)
{
//cin.get();
x1=pla[head].x;
y1=pla[head].y;
num1=pla[head].num;
// printf("进入第%d层\t 坐标(%d,%d)\n",num1,x1,y1);
if(map[num1][x1][y1]=='E')
{
//cout<<"找到出口!!!!"<<endl;
pla[tail].fa=head;
visited[num1][x1][y1]=1;
flag=1;
return ;
}
if(num1-1>=0&&map[num1-1][x1][y1]!='#'&&!visited[num1-1][x1][y1])//如果该行下一次可以遍历直接去下一层
{
// printf("上一层第%d层的坐标(%d,%d)可以进行遍历\n",num1-1,x1,y1);
visited[num1-1][x1][y1]=1;
pla[tail].fa=head;
pla[tail].num=num1-1;
pla[tail].x=x1;
pla[tail].y=y1;
tail++;
}
if(num1+1<L&&map[num1+1][x1][y1]!='#'&&!visited[num1+1][x1][y1])//如果该行下一次可以遍历直接去下一层
{
//printf("下一层第%d层的坐标(%d,%d)可以进行遍历!\n",num1+1,x1,y1);
visited[num1+1][x1][y1]=1;
pla[tail].fa=head;
pla[tail].num=num1+1;
pla[tail].x=x1;
pla[tail].y=y1;
tail++;
}
for(int i=0;i<4;i++)
{
x2=x1+dx[i];
y2=y1+dy[i];
num2=num1;
if(x2>=0&&x2<R&&y2>=0&&y2<C&&num2>=0&&num2<L)
if(map[num2][x2][y2]!='#'&&!visited[num2][x2][y2])
{
//printf("该第%d层的坐标(%d,%d)可以遍历\n",num2,x2,y2);
visited[num2][x2][y2]=1;
pla[tail].num=num2;
pla[tail].x=x2;
pla[tail].y=y2;
pla[tail].fa=head;
tail++;
}
}
head++;
//total++;
}
}
int main()
{
ios::sync_with_stdio(false);
int x,y,i,j,h,w;
while(cin>>L>>R>>C)
{
if(L==0||R==0||C==0)
break;
flag=0;
memset(visited,0,sizeof(visited));
memset(map,0,sizeof(map));
for(i=0;i<L;i++)
{
for(j=0;j<R;j++)
for(h=0;h<C;h++)
{
cin>>map[i][j][h];
if(map[i][j][h]=='S')
w=i,x=j,y=h;
}
cin.get();
}
total=0;
BFS(w,x,y);
while(head)
{
//cout<<"("<<pla[head].num<<","<<pla[head].x<<","<<pla[head].y<<")"<<endl;
head=pla[head].fa;
total++;
}
if(flag==0)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<total<<" minute(s)."<<endl;
}
return 0;
}