You are trapped in a 3D dungeon and need to nd the quickest way out! The dungeon is composed of unit cubes which may or may not be lled 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
The input le 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
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!
Sample Input
3 4 5
S….
.###.
.##..
###.#
#####
#####
##.##
##…
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
题意:
给定一个三维迷宫,S为起点,E为终点,#为石头,‘.’表示可以通行,有上下左右前后六个方向可以走,问能否走出迷宫,若能,输出走出迷宫需要的最短时间(每走一步一分钟)
思路:
用char型三维数组保存迷宫,bool型的三维数组记录每一点的访问情况,建立一个Cell结构体记录每一格的坐标和走到此处需要的步数,用bfs搜索
#include
#include
#include
#include
using namespace std;
int L,R,C;
int d[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};//上下、前后、左右六个方位
const int maxsize=35;
char dungeon3D[maxsize][maxsize][maxsize];//记录地牢
bool ifVisDungeon[maxsize][maxsize][maxsize];//记录每一格的访问情况
struct Cell
{
int l,r,c,step;//三维坐标和步数
};
queue | dungeon;
void initial()
{
Cell startCell;
memset(dungeon3D,'#',sizeof(dungeon3D));
memset(ifVisDungeon,false,sizeof(ifVisDungeon));
while(!dungeon.empty())
dungeon.pop();
for(int i=1;i<=L;i++)
{
for(int k=1;k<=R;k++)
{
for(int j=1;j<=C;j++)
{
cin>>dungeon3D[i][k][j];
if(dungeon3D[i][k][j]=='S')//初始化起始的坐标及步数
{
startCell.l=i;
startCell.r=k;
startCell.c=j;
startCell.step=0;
dungeon.push(startCell);//添加到队列中
}
}
}
}
}
void bfs()
{
Cell c1,c2;
int flag=0,steps=0;
while(!dungeon.empty())
{
c1=dungeon.front();
dungeon.pop();
ifVisDungeon[c1.l][c1.r][c1.c]=true;//标记为访问过
for(int i=0;i<6;i++)
{
c2.l=c1.l+d[i][0];
c2.r=c1.r+d[i][1];
c2.c=c1.c+d[i][2];
if(dungeon3D[c2.l][c2.r][c2.c]=='E')//到达终点
{
steps=c1.step+1;//步数加1
flag=1;//flag置为1
break;//跳出循环
}
if(!ifVisDungeon[c2.l][c2.r][c2.c])
{
ifVisDungeon[c2.l][c2.r][c2.c]=true;
if(dungeon3D[c2.l][c2.r][c2.c]=='.')
{
c2.step=c1.step+1;//步数加1
dungeon.push(c2);
}
}
}
if(flag)
break;
}
if(flag)
cout<<"Escaped in "<" minute(s)."<else
cout<<"Trapped!"<int main()
{
while(cin>>L>>R>>C)
{
if(L==0&&R==0&&C==0)
break;
initial();
bfs();
}
return 0;
}