POJ 2251题解 Dungeon Master -kuangbinB题(题解·)

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!

分析:三维的广搜题,广搜过程没有新意,但是三维空间的考虑着实让我思考了一阵。一开始考虑空行问题,三维移动等各种因素,让我不知道怎么用三维数组,强制用二维数组模拟又拿捏不准。导致失误。经过思索,还是用三维数组,int dx[6] = { 1,0,-1,0,0,0 }, dy[6] = { 0,1,0,-1,0,0 }, dz[6] = { 0,0,0,0,1,-1 };用三个数组来模拟三维移动;

具体代码如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<queue>
#include<math.h>
using namespace std;
const int maxn = 35;
char maze[maxn][maxn][maxn];
int visit[maxn][maxn][maxn];
int L, R, C;
int ax, ay, az, gx, gy, gz;
int dx[6] = { 1,0,-1,0,0,0 }, dy[6] = { 0,1,0,-1,0,0 }, dz[6] = { 0,0,0,0,1,-1 };
typedef struct uuuu
{
	int first;
	int second;
	int stage;
}P;
int bfs() {

	int i;
	queue<P> que;
	P a;
	a.first = ax; a.second = ay; a.stage = az;
	que.push(a);
	//que.push(P{ ax, ay, az });
	visit[ax][ay][az] = 0;
	while (que.size())
	{
		P p = que.front(); que.pop();
		if (p.first == gx && p.second == gy && p.stage == gz)
		{
			break;
		}
		for (i = 0; i < 6; i++)
		{
			int nx = p.first + dx[i], ny = p.second + dy[i];
			int nz = p.stage + dz[i];
			if (nx >= 0 && nx < R && ny >= 0 && ny < C && nz >= 0 && nz < L
				&& !visit[nx][ny][nz] && maze[nx][ny][nz] != '#')
			{
				P b;
				b.first = nx; b.second = ny; b.stage = nz;
				que.push(b);
				//que.push(P{ nx,ny,nz });
				visit[nx][ny][nz] = visit[p.first][p.second][p.stage] + 1;
			}
		}
	}
	return visit[gx][gy][gz];

}
int main()
{
	int i, j, k;
	while (cin >> L >> R >> C)
	{
		memset(visit, 0, sizeof(visit));
		if (L == 0 && R == 0 && C == 0)
		{
			break;

		}
		for (k = 0; k < L; k++)
			for (i = 0; i<R; i++)
				for (j = 0; j<C; j++)
				{
					cin >> maze[i][j][k];
					if (maze[i][j][k] == 'S')
					{
						ax = i; ay = j; az = k;
					}
					if (maze[i][j][k] == 'E')
					{
						gx = i; gy = j; gz = k;
					}
				}
		int res = bfs();
		if (res)
		{
			printf("Escaped in %d minute(s).\n", res);
		}
		else
		{
			cout << "Trapped!" << endl;
		}
	}
	return 0;
}

注意:

1.输出格式一个.让我找了半个小时BUG。细心为上。

2.队列对结构体的输入问题也需注意,有些编译器(包括判别)不允许que.push(P{ ax, ay, az });中括号应先给结构体各值赋值在push队列。

猜你喜欢

转载自blog.csdn.net/fighting_yifeng/article/details/81219112