POJ 2251 Dungeon Master

题目链接: POJ 2251

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!

题意

一个3D版本的迷宫,输入3维坐标的层数,然后录入迷宫,如果能出去就输出Escaped in X minute(s).X是出去的时间,如果不能的话就输出Trapped!

题解:

典型的迷宫问题水题,只是从2维变成了3维,代码没有太多的不同,还是用队列来做,点坐标的话,最好用结构体存起来,书写起来更清晰些

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>

using namespace std;

typedef long long ll;

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int N = 35;
char feld[N][N][N];
int L, R, C;
int sx, sy, sz, gx, gy, gz;
struct Node {
	int x;
	int y;
	int z;
	Node(int a, int b, int c) {
		x = a;
		y = b;
		z = c;
	}
};
int d[N][N][N];
int dir[6][3] = { 0,0,1,0,1,0,1,0,0,0,0,-1,0,-1,0,-1,0,0 };
void bfs() {
	queue<Node>P;
	Node temp(sx, sy, sz);
	P.push(temp);
	d[sx][sy][sz] = 0;
	feld[sx][sy][sz] = '#';
	int nx, ny, nz;
	while (!P.empty()) {
		temp = P.front();
		P.pop();
		if (temp.x == gx && temp.y == gy && temp.z == gz)
			break;
		for (int i(0); i < 6; i++) {
			nx = temp.x + dir[i][0];
			ny = temp.y + dir[i][1];
			nz = temp.z + dir[i][2];
			if (nx >= 0 && ny >= 0 && nz >= 0 && nx < L&&ny < R&&nz < C&&feld[nx][ny][nz]!= '#') {
				d[nx][ny][nz] = d[temp.x][temp.y][temp.z] + 1;
				feld[nx][ny][nz] = '#';
				P.push(Node(nx, ny, nz));
			}
		}
	}
}
int main() {
	while (cin >> L >> R >> C,L) {
		memset(feld, 0, sizeof feld);
		memset(d, -1, sizeof d);
		for (int i(0); i < L; i++)
			for (int j(0); j < R; j++) {
				cin >> feld[i][j];
				for (int k(0); k < C; k++) {
					if (feld[i][j][k] == 'S') {
						sx = i;
						sy = j;
						sz = k;
					}
					else if (feld[i][j][k] == 'E') {
						gx = i;
						gy = j;
						gz = k;
					}
				}
			}
		bfs();
		if (d[gx][gy][gz] == -1)
			cout << "Trapped!" << endl;
		else cout << "Escaped in " << d[gx][gy][gz] << " minute(s)." << endl;

	}
}
原文地址:https://www.cnblogs.com/Titordong/p/9588409.html