Description
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
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
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’为终点,‘.’表示能走,‘#’表示不能走,如果能从起点走到终点,就输出最少走多少步,如果无法到达终点,输出‘Trapped!’。
题解:正常bfs就行
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=50;
int l,r,c;
char map[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
bool flag;//记录能否到达终点
struct edge{
int x,y,z;
int s;
};
queue<edge> q;
int main()
{
while(1)
{
scanf("%d%d%d",&l,&r,&c);
if(l==0&&r==0&&c==0)
{
break;
}
edge start;
memset(map,'\0',sizeof(map));
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='S')
{
flag=1;
start.x=i,start.y=j,start.z=k;
start.s=0;
}
}
}
}
while(!q.empty())
{
q.pop();
}
memset(vis,0,sizeof(vis));
q.push(start);
vis[start.x][start.y][start.z]=1;
while(!q.empty())
{
edge v=q.front();
q.pop();
if(map[v.x][v.y][v.z]=='E')
{
flag=0;
printf("Escaped in %d minute(s).\n",v.s);
break;
}
v.s++;
edge u;
if(v.x-1>=1)
{
u=v;
u.x=v.x-1;
if(!vis[u.x][u.y][u.z]&&map[v.x][v.y][v.z]!='#')
{
q.push(u);
vis[u.x][u.y][u.z]=1;
}
}
if(v.x+1<=l)
{
u=v;
u.x=v.x+1;
if(!vis[u.x][u.y][u.z]&&map[v.x][v.y][v.z]!='#')
{
q.push(u);
vis[u.x][u.y][u.z]=1;
}
}
if(v.y-1>=1)
{
u=v;
u.y=v.y-1;
if(!vis[u.x][u.y][u.z]&&map[v.x][v.y][v.z]!='#')
{
q.push(u);
vis[u.x][u.y][u.z]=1;
}
}
if(v.y+1<=r)
{
u=v;
u.y=v.y+1;
if(!vis[u.x][u.y][u.z]&&map[v.x][v.y][v.z]!='#')
{
q.push(u);
vis[u.x][u.y][u.z]=1;
}
}
if(v.z-1>=1)
{
u=v;
u.z=v.z-1;
if(!vis[u.x][u.y][u.z]&&map[v.x][v.y][v.z]!='#')
{
q.push(u);
vis[u.x][u.y][u.z]=1;
}
}
if(v.z+1<=c)
{
u=v;
u.z=v.z+1;
if(!vis[u.x][u.y][u.z]&&map[v.x][v.y][v.z]!='#')
{
q.push(u);
vis[u.x][u.y][u.z]=1;
}
}
}
if(flag)
{
printf("Trapped!\n");
}
}
return 0;
}