码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2251:Dungeon Master【bfs】

时间:2015-08-06 18:27:36      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:bfs   poj   

Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 7
Problem 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!

AC-code:

#include<cstdio>
#include<cstring>
#include<queue>
#define INF 0xffffff
int ans,l,r,c,ex,ey,ez,vis[35][35][35];
char a[35][35][35];
int dx[6]={0,0,0,0,1,-1};
int dy[6]={1,-1,0,0,0,0};
int dz[6]={0,0,1,-1,0,0};
using namespace std;

struct node 
{
	int x,y,z,step;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
}temp,p;
void bfs(int x1,int y1,int z1)
{
	p.x=x1;
	p.y=y1;
	p.z=z1;
	p.step=0;
	vis[p.x][p.y][p.z]=1;
	priority_queue<node>q;
	q.push(p);
	while(!q.empty())
	{
		p=q.top();
		q.pop();
		for(int i=0;i<6;i++)
		{
			temp.x=p.x+dx[i];
			temp.y=p.y+dy[i];
			temp.z=p.z+dz[i];
			temp.step=p.step+1;
			if(!vis[temp.x][temp.y][temp.z]&&temp.x>=0&&temp.x<l&&temp.y>=0&&temp.y<r&&temp.z>=0&&temp.z<c)
			{
				if(temp.x==ex&&temp.y==ey&&temp.z==ez)
				{
					printf("Escaped in %d minute(s).\n",temp.step);
					return ;
				}
				vis[temp.x][temp.y][temp.z]=1;
				q.push(temp);
			}
		}
	}
	printf("Trapped!\n");
}
int main()
{
	int i,j,k,x,y,z;
	while(scanf("%d%d%d",&l,&r,&c),l||r||c)
	{
		memset(vis,0,sizeof(vis));
		for(i=0;i<l;i++,getchar())
		{
			for(j=0;j<r;j++)
			{
				getchar();
				for(k=0;k<c;k++)
				{
					scanf("%c",&a[i][j][k]);
					if(a[i][j][k]=='S')
					{
						x=i;
						y=j;
						z=k;
					}
					else if(a[i][j][k]=='E')
					{
						ex=i;
						ey=j;
						ez=k;
					}
					else if(a[i][j][k]=='#')
						vis[i][j][k]=1;
					else
						vis[i][j][k]=0;
				}
			}
		}
		ans=INF;
		bfs(x,y,z);		
	}
	return 0;
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2251:Dungeon Master【bfs】

标签:bfs   poj   

原文地址:http://blog.csdn.net/lin14543/article/details/47319711

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!