POJ 2251 Dungeon Master

本文最后更新于:2020年6月10日 下午

【POJ】 2251 Dungeon Master (BFS)

题目链接: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!

题目理解

​ 给定一个三维的迷宫,障碍物用#表示,迷宫中给定起点S终点E,判断是否能够从S走到E,如果能,返回所需最短路径长度。使用BFS。


代码

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

struct node{
    int x, y, z;
};
string map3D[32][32];
int vis[32][32][32];
node Begin, End;
queue<node> q;
int L, R, C, ans;
int dx[] = {1, -1, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};

int BFS(){
	q.push(Begin);
	while(!q.empty()){
		node cur = q.front();
		q.pop();
		if(cur.x == End.x && cur.y == End.y && cur.z == End.z)
			return vis[End.x][End.y][End.z];
		for(int i = 0; i < 6; ++i){
			node temp;
			temp.x = cur.x + dx[i];
			temp.y = cur.y + dy[i];
			temp.z = cur.z + dz[i];
			if(temp.x >= 0 && temp.y >=0 && temp.z >= 0 && temp.x < L && temp.y < R && temp.z < C && map3D[temp.x][temp.y][temp.z] != '#' && !vis[temp.x][temp.y][temp.z]){
				q.push(temp);
				vis[temp.x][temp.y][temp.z] = vis[cur.x][cur.y][cur.z] + 1;
			}
		}
	}
	return 0;
}

int main(){
	while(cin >> L >> R >> C && L != 0 && R != 0 && C != 0){
		memset(vis, 0, sizeof(vis));
		while(!q.empty())
			q.pop();
		for(int i = 0; i < L; ++i)
			for(int j = 0; j < R; ++j){
				cin >> map3D[i][j];
				for(int k = 0; k < C; ++k){
					if(map3D[i][j][k] == 'S')
						Begin.x = i, Begin.y = j, Begin.z = k;
					else if(map3D[i][j][k] == 'E')
						End.x = i, End.y = j, End.z = k;
				}
			}
		ans = BFS();
		if(ans)
			cout << "Escaped in " << ans << " minute(s)." << endl;
		else
			cout << "Trapped!" << endl;
	}
	return 0;
} 

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!