您的位置:首页 > 其它

POJ 2251 Dungeon Master(三维bfs)

2017-01-07 00:58 429 查看
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!

第一次做三维的bfs,刚开始一只看不懂样例..最后才搞懂第一个l表示的是层数,样例中的三个图分别表示第一层、第二层、第三层;

另外注意i,j,k循环时 i表示z轴,j表示x轴,k表示y轴

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<queue>
int dire[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
int dire2[8][2]={{-1,-1},{-1,0},{-1,1},{ 0,-1},{ 0,1},{ 1,-1},{ 1,0},{ 1,1}};
#define rep(i,a,b) for(int i=(a);i<=(b);(i++))
#define inf 0x3f3f3f
#define ll long long
#define pi acos(-1)
int dire3[6][3]={ {0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0} };
using namespace std;
struct node{
int x,y,z;
node(int x=0,int y=0,int z=0):x(x),y(y),z(z){}
/*  重载操作符 == ,判断Node相等 */
bool operator == (const node& rhs) const {
return x == rhs.x && y == rhs.y && z == rhs.z;
}
}S,E;

char g[33][33][33];
int dist[33][33][33];
int l,r,c;
int bfs(node S){
queue<node> q;
q.push(S);
memset(dist,-1,sizeof(dist));
dist[S.z][S.x][S.y]=0;
while(!q.empty()){
node u=q.front();
q.pop();
if(u==E)
return dist[u.z][u.x][u.y];
for(int i=0;i<6;i++){
int x=u.x+dire3[i][0];
int y=u.y+dire3[i][1];
int z=u.z+dire3[i][2];

if(z<0||z>=l||x<0||x>=r||y<0||y>=c)
continue;
node v=node(x,y,z);

if(g[z][x][y]=='.' &&dist[z][x][y]==-1){
dist[z][x][y] =dist[u.z][u.x][u.y]+1;
q.push(v);
}
}
}
return -1;
}

int main(){

while(cin>>l>>r>>c){
if(!l &&!r &&!c) break;

for(int i=0;i<l;i++)
for(int j=0;j<r;j++) scanf("%s",g[i][j]);
for(int i=0;i<l;i++)
for(int j=0;j<r;j++)
for(int k=0;k<c;k++){
if(g[i][j][k]=='S')
S=node(j,k,i),g[i][j][k]='.';
if(g[i][j][k]=='E')
E=node(j,k,i),g[i][j][k]='.';
}
int ans=bfs(S);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: