POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)

时间:2022-07-22 17:40:31
Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 48380   Accepted: 18252

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

跟平常的bfs问题的区别就是方向有6个

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 35
using namespace std;
int dir[][]={{,,},{,,},{,,-},{,-,},{,,},{-,,}};//6个方向 东,南,西,北,上,下
char G[max_v][max_v][max_v];
int vis[max_v][max_v][max_v];
int sx,sy,sz,fx,fy,fz;
int n,m,k;
int ans;
struct node
{
int x,y,z;
int step;
};
bool check(node a)//检查该点合法性
{
if(a.x<||a.x>=n||a.y<||a.y>=m||a.z<||a.z>=k)
return false;
else if(G[a.z][a.x][a.y]=='#')
return false;
else if(vis[a.z][a.x][a.y])
return false;
else
return true;
}
void bfs(int x,int y,int z)
{
queue<node> q;
node p,next; p.x=x;
p.y=y;
p.z=z;
p.step=; q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==fx&&p.y==fy&&p.z==fz)
{
ans=p.step;
return ;
} for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][];
next.z=p.z+dir[i][]; if(check(next))
{
vis[next.z][next.x][next.y]=;
next.step=p.step+;
q.push(next);
} }
}
}
int main()
{
while(~scanf("%d %d %d",&k,&n,&m))
{
if(n==&&m==&&k==)
break; for(int z=;z<k;z++)
{
for(int i=;i<n;i++)
{
scanf("%s",G[z][i]);
for(int j=;j<m;j++)
{
if(G[z][i][j]=='S')
sx=i,sy=j,sz=z;
else if(G[z][i][j]=='E')
fx=i,fy=j,fz=z;
}
}
} memset(vis,,sizeof(vis));
ans=-; bfs(sx,sy,sz); if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return ;
}