POJ - 2251 Dungeon Master (三维地图内的 bfs 找最短距离)
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!
题目大意:
给你个h,n,m表示 这个三维图地图的高 长 宽,让你找 从S(起点) 到 E(终点)最少需要多少步,#表示石头 不能走。
解题思路:
就把普通的二维地图变成三维就好了,存地图的数组变成三维的。
然后每一次所能走的方向 变成了 前 后 左 右 上 下 六个方向,这时候我们的next数组要
int Next[6][3]={0,0,1,0,1,0,1,0,0,0,0,-1,0,-1,0,-1,0,0};
这么定义。
每一步我们用一个结构体来记录坐标 x y z和走了多少步 step。这样bfs 一直到终点就是最短的步数。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 33;
char mp[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int n,m,h;
int Next[6][3]={0,0,1,0,1,0,1,0,0,0,0,-1,0,-1,0,-1,0,0};
struct node{
int x,y,z;
int step;
node(){}
node(int X,int Y,int Z,int S):x(X),y(Y),z(Z),step(S){}
};
node st,ed;
void init(){
memset(vis,0,sizeof(vis));
}
void show(){
for(int i=1;i<=h;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=m;k++){
cout<<mp[j][k][i];
}
cout<<endl;
}
cout<<endl;
}
}
int bfs(int x,int y,int z){
vis[x][y][z]=1;
queue<node> q;
q.push(node(x,y,z,0));
while(q.size()){
node now = q.front();
q.pop();
int xx=now.x,yy=now.y,zz=now.z;
for(int i=0;i<6;i++){
int tx=xx+Next[i][0];
int ty=yy+Next[i][1];
int tz=zz+Next[i][2];
if(tx<=0||tx>n||ty<=0||ty>m||tz>h||tz<=0) continue;
if(vis[tx][ty][tz]) continue;
if(mp[tx][ty][tz]=='#') continue;
if(tx==ed.x&&ty==ed.y&&tz==ed.z){
return now.step+1;
}
vis[tx][ty][tz]=1;
int num = now.step+1;
q.push(node(tx,ty,tz,num));
}
}
return -1;
}
int main(){
while(cin>>h>>n>>m){
if(n==0&&m==0&&h==0) break;
init();
for(int k=1;k<=h;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mp[i][j][k];
if(mp[i][j][k]=='S'){
st.x=i,st.y=j,st.z=k;
}else if(mp[i][j][k]=='E'){
ed.x=i,ed.y=j,ed.z=k;
}
}
}
getchar();
}
int ans = bfs(st.x,st.y,st.z);
if(ans==-1) puts("Trapped!");
else{
printf("Escaped in %d minute(s).\n",ans);
}
}
return 0;
}