POJ 2251 Dungeon Master

Time limit 1000 ms
Memory limit 65536 kB

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!

题目描述   

       这个题的核心思想就是广度搜索,广度搜索的思路就是从一个点开始,向多个方向扩散,且每次选取的用于向四周扩散的点都是队列中所有可以扩散的点中移动步数最小的,所谓的扩散就是如下图所示,我们先将所有步数最小的点做为扩散点,一个个的完成向四周的扩散。

 

 

 

 

 

 

 

 

 

 

 

 

 

步数:2

 

 

 

 

 

 

 

步数:2

 

步数:1

步数:2

 

 

 

 

 

 

步数:2

 

步数:1

 

 

这是起点

步数:1

 

步数:2

 

 

 

 

 

 

步数:2

 

步数:1

 

步数:2

 

 

 

 

 

 

 

 

步数:2

 

 

 

 

 

 

 

 

 

 

 

 

 

       我们的思路就是,先将所有从起点出发,需要走1步的点全部找出来,然后再将需要走两步的点全部找出来。由于是走最短路,所以往回走是不能找到最短路的,你或许会想,要是上图中,从步数1的点出发走到了同为步数1的点的话,那会怎么样,不过,请想一想,我们求的是最短路线,在某一条路线上用n步可以走到某一个点,那我们又何必在另外一条路线上用n+1步走到这个点你?

       以上就是广度搜索的基本思想,按我的理解,就是一个实现从起点开始扩散,现在我们来讨论一个这个题目

      这个题是要求的是最短路,很明显,这就是一个广度搜索就行了,为了方便,我们将数据存在结构体中,并给与对应于其属性的构造器,这样的目的是为了简化代码,通过调用函数来实现结构体属性的赋值,这样符合封装性的特点,但是为了方便使用其属性,所以属性没有写成private型,而是缺省型。

       这个题和我画的图不同的地方在于,这个图不是二维而是三维的,但是这不要紧,我们只需要将移动的方向改成对应于三维的移动即可,也就是上下,左右,前后,六个方向,只要理解了广度搜索的基本使用,这个题应该不成问题了。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std;

int l, r, c;
char map[31][31][31];

//代表移动的四个方向
int to[6][3] = { {1,0,0} , {0,1,0} , {0,0,1} , {-1,0,0} , {0,-1,0} , {0,0,-1} };

typedef struct Dot
{
	int x;
	int y;
	int z;
	int step;
public :
	//带参的构造器
	Dot(int x,int y,int z,int step)
	{
		this->x = x;
		this->y = y;
		this->z = z;
		this->step = step;
	}
	//空参构造器
	Dot()
	{
		x = 0;
		y = 0;
		z = 0;
		step = 0;
	}
}Dot;

int bfs(int tx, int ty, int tz);

int main()
{
	while(~scanf("%d%d%d",&l,&r,&c) && l+r+c)
	{
		int sx, sy, sz;						//记录起点的位置
		for(int i = 0 ;i < l ; i ++)
		{
			for(int j = 0 ;j < r ; j ++)
			{
				for(int k = 0 ;k < c ; k ++)
				{
					cin >> map[i][j][k];
					if(*(*(*(map+i) + j) + k) == 'S')	//这个地方要进行多次比较,为了节省时间,用了指针来代替[]
					{
						sx = i;
						sy = j;
						sz = k;
					}
				}
			}
		}
		int sum = bfs(sx, sy, sz);
		if(sum == -1)
		{
			printf("Trapped!
");
		}
		else
		{
			printf("Escaped in %d minute(s).
", sum);
		}

	}
	return 0;
}

int bfs(int x,int y,int z)
{
	queue<Dot>way;
	Dot now(x,y,z,0);						//使用构造函数,给结构体的属性赋值
	way.push(now);							//将第一个存入队列

	while(!way.empty())						//广度搜素
	{
		now = way.front();					//从队列中取出一个点,由于入队顺序的原因,每次取出的总是步数最小的
		way.pop();							//用了就要放出来
		for(int i = 0 ; i < 6 ; i ++)		//六个方向
		{
			
			int tx = now.x + to[i][0];
			int ty = now.y + to[i][1];
			int tz = now.z + to[i][2];		//搜索不同的方向
			//判断是否可走
			if(map[tx][ty][tz] == 'E')		//到了终点
			{
				return now.step + 1;		//返回所用步数
			}
			if(map[tx][ty][tz] == '.' &&  0<=tx && tx <= l && 0<=ty && ty <= r && 0 <= tz && tz <= c)
			{
				map[tx][ty][tz] = '#';		//避免重复,标记为不可走,也就是已经走过了
				Dot temp(tx, ty, tz, now.step + 1);	//新的迁移点,记得步数加一
				way.push(temp);				//将新的迁移点存入队列
			}
		}
	}
	return -1;								//这表示已经走不到终点了
}
原文地址:https://www.cnblogs.com/winter-bamboo/p/10634456.html