poj 2251 Dungeon Master(bfs)

时间:2021-09-08 16:34:42
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!

Source

题意:一个3D的迷宫,问能否逃出去
直接bfs。。。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<stdlib.h>
using namespace std;
int k,n,m;
char map[][][];
int vis[][][];
struct Node
{
int floor;
int x,y;
int t;
}st,ed;
int dirx[]={,,-,};
int diry[]={-,,,};
void bfs(Node s)
{
queue<Node>q;
q.push(s);
vis[s.floor][s.x][s.y]=;
Node t1,t2;
while(!q.empty())
{
t1=q.front();
q.pop();
if(t1.floor==ed.floor && t1.x==ed.x && t1.y==ed.y)
{
printf("Escaped in %d minute(s).\n",t1.t);
return;
} for(int i=;i<;i++)
{
t2.floor=t1.floor;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(t2.x>= && t2.x<n && t2.y>= && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!='#')
{
vis[t2.floor][t2.x][t2.y]=;
t2.t=t1.t+;
q.push(t2);
}
}
t2=t1;
t2.floor=t1.floor+;
if(t2.floor< || t2.floor>=k) continue;
if(t2.x>= && t2.x<n && t2.y>= && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!='#')
{
vis[t2.floor][t2.x][t2.y]=;
t2.t=t1.t+;
q.push(t2);
} t2=t1;
t2.floor=t1.floor-;
if(t2.floor< || t2.floor>=k) continue;
if(t2.x>= && t2.x<n && t2.y>= && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!='#')
{
vis[t2.floor][t2.x][t2.y]=;
t2.t=t1.t+;
q.push(t2);
}
}
printf("Trapped!\n");
}
int main()
{
while(scanf("%d%d%d",&k,&n,&m)== && n+m+k!=)
{
for(int i=;i<k;i++)
{
for(int j=;j<n;j++)
{
scanf("%s",map[i][j]);
for(int l=;l<m;l++)
{
if(map[i][j][l]=='S')
{
st.floor=i;
st.x=j;
st.y=l;
st.t=;
}
if(map[i][j][l]=='E')
{
ed.floor=i;
ed.x=j;
ed.y=l;
}
}
}
}
memset(vis,,sizeof(vis));
bfs(st);
}
return ;
}