poj 2251 Dungeon Master

Dungeon Master

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

Sample Input
 
   
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
 

Sample Output
 
   
Escaped in 11 minute(s). Trapped!
 

Source
PKU
 
题目描述:
              刚开始输入三个数,L,R,C分别代表层数,行数,列数,然后输入L*R*C个字符,字符S代表起始位置,字符E代表终点位置,字符.代表路,字符#代表墙,让你求是否起点能够走到终点,如果能的话,就输出最短的时间,如果不能就数出一句话!因为每走一步用时1分钟,所以所求的时间和所走的路程大小相同!
注意:在输入的时候要进行空行,所以要多加一个getchar()来吸收换行符!
思路: 
       这道题是一道空间上的求最短路径问题,比二维坐标上的又上升了一个高度,但是思想是大体一样的,只需要我们将部分程序改动一下就行了!具体做法看程序:
 
代码:
 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0xfffffff
char map[35][35][35];
int vis[35][35][35];
int x,y,z,ex,ey,ez;
int ans,flag,L,R,C;
int dx[8]={0,1,-1,0,0,0,0,0};
int dy[8]={1,0,0,-1,0,0,0,0};
int dz[8]={0,0,0,0,0,1,-1,0};
using namespace std;
struct node //因为要用到结构体优先队列,所以结构体定义的时候要这样定义 
{
	int x,y,step;
	int z;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;//按照步数从小到大排列 
	}
}a,temp;
int judge()//判断是否符合条件 
{
	if(temp.x<1||temp.x>L)	return 0;
	if(temp.y<1||temp.y>R)	return 0;
	if(temp.z<1||temp.z>C)  return 0;
	if(map[temp.x][temp.y][temp.z]=='#')  return 0;
	if(vis[temp.x][temp.y][temp.z]==1)  return 0;
	if(temp.step>=ans)	return 0;
	return 1;
}

void bfs()
{
	a.x=x;//将起点赋值给结构体a 
	a.y=y;
	a.z=z;
	a.step=0;
	memset(vis,0,sizeof(vis));//对标记数组清0处理 
	priority_queueq;//定义一个优先队列! 
	vis[x][y][z]=1;//标记一下起点 
	q.push(a);//将结构体a装到队列中 
	while(!q.empty())//当队列不为空的时候进行下面的操作 
	{
		a=q.top();//将对顶元素赋值给结构体,对其进行查找周围的元素,看看是不是终点 
		q.pop();
		for(int i=0;i<8;i++)
		{
			temp.x=a.x+dx[i];
			temp.y=a.y+dy[i];
			temp.z=a.z+dz[i];
			temp.step=a.step+1;
			if(judge())//如果周围的元素有符合条件的 
			{
				if(temp.x==ex&&temp.y==ey&&temp.z==ez)//先判断是不是终点,是的话直接输出 
				{
					ans=temp.step;
					flag=1;
					return;
				}
				vis[temp.x][temp.y][temp.z]=1;//不是的话将其标记,然后放到队列中,重新进行循环 
				q.push(temp);//来找终点 
			}
		}
	}
}
int main()
{
	while(scanf("%d%d%d",&L,&R,&C)&&(L||R||C))//输入层数,行数,列数,来确定字符的个数 
	{
		ans=INF;flag=0;//ans代表步数的最小值,flag用来确定是否有路能够到达终点 
		getchar();//吸收换行符! 
		for(int i=1;i<=L;i++)//输入字符! 
		{
			for(int j=1;j<=R;j++)
			{
				for(int k=1;k<=C;k++)
					{
					  scanf("%c",&map[i][j][k]);
					  if(map[i][j][k]=='S')//找起点 
				      {
				       	x=i;y=j;z=k;
				      }
				      else if(map[i][j][k]=='E')//找终点 
				      {
				      	ex=i;ey=j;ez=k;
				      }
				    }
				    getchar();
			}
			getchar();//吸收换行符,做用空一行 
		}
		bfs();//来找最短路径 
		if(flag==1)//如果找到,则输出最小的时间 
		printf("Escaped in %d minute(s).\n",ans);
		else
		printf("Trapped!\n");
	}
	return 0;
}

         

你可能感兴趣的:(bfs)