您的位置:首页 > 其它

POJ 2251 Dungeon Master

2016-07-22 11:48 405 查看
Time Limit: 1000MS Memory Limit: 65536K

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!

思路:

一个三维的迷宫问题,要求从起点S走到终点E的最短路径,当然也可能不存在。一开始我想到的是采用深搜来做,但是超时了;

广搜思路:我们用一个队列来存放将来要访问的节点,首先从起点开始,将起点节点放入队列中,然后每次把队列头节点出队列,将此节点六个方向进判断,若在空间内且没访问过,而且不是墙壁,那么就将此节点放入队列中,步数相应加一。这样,我们根据队列先进先出的性质,可行路线肯定是按步数从小到大排列。最先符合要求的就是最短路线。直到所有可以走的地方都走过了,还没有符合要求的路线,那么就不存在。

AC代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int l,r,c,visit[50][50][50];
int dir[6][3]={{0,1,0},{1,0,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};
char s[50][50][50];

typedef struct road
{
int x,y,z;
}Node;
Node start,last;

bool feasible(int x,int y,int z)
{
if(x>=0&&x<r&&y>=0&&y<c&&z>=0&&z<l&&s[x][y][z]=='.'&&visit[x][y][z]==-1)
return true;
return false;
}
void bfs()
{
queue<Node>  h;
Node tmp;
h.push(start);
visit[start.x][start.y][start.z]=0;
while(!h.empty()){
Node cur;
cur=h.front();
if(cur.x==last.x&&cur.y==last.y&&cur.z==last.z)
{
printf("Escaped in %d minute(s).\n",visit[cur.x][cur.y][cur.z]);
return;
}
h.pop();
for(int i=0;i<6;i++){
int cux=cur.x+dir[i][0];
int cuy=cur.y+dir[i][1];
int cuz=cur.z+dir[i][2];
if(feasible(cux,cuy,cuz))//判断是否符合要求
{
visit[cux][cuy][cuz]=visit[cur.x][cur.y][cur.z]+1;//步数+1,只要不为-1就是被访问过的。
tmp.x=cux;tmp.y=cuy;tmp.z=cuz;
h.push(tmp);
}
}
}
printf("Trapped!\n");return ;
}
int main()
{
int i,j,k;
char blank[100];
while(~scanf("%d%d%d",&l,&r,&c))
{
if(l==0&&r==0&&c==0)
break;
for(i=0;i<l;i++){
for(j=0;j<r;j++){
getchar();
for(k=0;k<c;k++)
{
scanf("%c",&s[j][k][i]);visit[j][k][i]=-1;
if(s[j][k][i]=='S'){//将起点终点记录下来
start.x=j;start.y=k;start.z=i;}
else if(s[j][k][i]=='E'){
last.x=j;last.y=k;last.z=i;s[j][k][i]='.';}
}
}
gets(blank);
}
//gets(blank);//最后一行要读取换行符和空行
bfs();
}
return 0;
}


采用深搜Time Limit Exceeded代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 999999999
int minu=INF,road=0,l,r,c,start[3],last[3];
int dir[6][3]={{0,1,0},{1,0,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};

struct road
{
char str[50][50];
}s[100],board[100];

bool feasible(int d,int a,int b)
{
if(a>=0&&a<r&&b>=0&&b<c&&d>=0&&d<l&&!board[d].str[a][b])
return true;
else
return false;
}
void dfs(int z,int x,int y,int step)
{
int i;
//printf("x=%d y=%d z=%d\n",x,y,z);
if(z==last[0]&&x==last[1]&&y==last[2])
{
//  printf("1037\n");
if(minu>step)
{
minu=step;
}
return ;
}
else
{
int xx,yy,zz,h;
for(h=0;h<6;h++){
xx=x+dir[h][0];
yy=y+dir[h][1];
zz=z+dir[h][2];
//  printf("1111111\n");
if(feasible(zz,xx,yy)&&s[zz].str[xx][yy]=='.')
{
//printf("str=%c\n",s[zz].str[xx][yy]);
//printf("xx=%d yy=%d zz=%d\n",xx,yy,zz);
board[zz].str[xx][yy]=1;
dfs(zz,xx,yy,step+1);
board[zz].str[xx][yy]=0;
}
}
//  printf("不符\n");
}
return ;
}

int main()
{
int i,j,k;
char blank[100];
while(~scanf("%d%d%d",&l,&r,&c))
{
minu=INF;
memset(board,0,sizeof(board));
if(l==0&&r==0&&c==0)
break;
for(i=0;i<l;i++){
for(j=0;j<r;j++){
getchar();
for(k=0;k<c;k++)
{
scanf("%c",&s[i].str[j][k]);
board[i].str[j][k]=0;
if(s[i].str[j][k]=='S'){
start[0]=i;start[1]=j;start[2]=k;}
else if(s[i].str[j][k]=='E')
{
last[0]=i;last[1]=j;last[2]=k;
s[i].str[j][k]='.';
}
}
}
gets(blank);
}
gets(blank);
//for(i=0;i<3;i++)
//printf("%d .",last[i]);
dfs(start[0],start[1],start[2],0);
if(minu!=999999999)
printf("Escaped in %d minute(s).\n",minu);
else
printf("Trapped!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ-2251 广搜