您的位置:首页 > 其它

POJ-2251-Dungeon Master(BFS 优先队列)

2016-06-06 15:52 459 查看
B - Dungeon Master

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

POJ 2251

Appoint description:

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!

走过的路标记为#,时间少的先出队,自定义六个搜索方向,直至搜到出口或者队列为空

代码

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=33;
char map[maxn][maxn][maxn];
int dis[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
//搜索方向
int star_x;
int star_y;
int star_z;//起始点坐标
int end_x;
int end_y;
int end_z;
int L,R,C;
struct node
{
int x;
int y;
int z;
int time;
friend bool operator <(node n1,node n2)
{
return n1.time>n2.time;
}
};
bool Judge(node end)
{
if(end.x>0&&end.x<=L&&end.y>0&&end.y<=R&&end.z>0&&end.z<=C&&map[end.x][end.y][end.z]!='#')//搜索坐标未越界,且可被搜索
return true;
return false;
}
void BFS()
{
node star;
star.time=0;
star.x=star_x;
star.y=star_y;
star.z=star_z;
map[star.x][star.y][star.z]='#';
priority_queue<node>q;
q.push(star);
while(!q.empty())
{
star=q.top();
q.pop();
if(star.x==end_x&&star.y==end_y&&star.z==end_z)
{
printf("Escaped in %d minute(s).\n",star.time);
return;
}
for(int i=0; i<6; i++) //六个搜索方向
{
node end;
end.x=star.x+dis[i][0];
end.y=star.y+dis[i][1];
end.z=star.z+dis[i][2];
if(Judge(end))
{
//                printf("*****\n");
//                printf("%d %d %d\n",end.x,end.y,end.z);//打印搜索路径
end.time=star.time+1;
map[end.x][end.y][end.z]='#';
q.push(end);

}
}
}
printf("Trapped!\n");

}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C)&&L&&R&&C)
{
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')
{
star_x=i;
star_y=j;
star_z=k;
}
if(map[i][j][k]=='E')
{
end_x=i;
end_y=j;
end_z=k;
//                        printf("%d %d %d\n",i,j,k);
}
}
//                getchar();
}
}
BFS();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: