POJ-2251 Dungeon Master(三维空间BFS)

本文详细解析了一个三维迷宫逃脱问题的算法实现,通过BFS(宽度优先搜索)算法寻找从起点到出口的最短路径。文章介绍了如何利用队列进行节点遍历,通过方向数组确定移动方向,并使用标记数组避免重复访问同一位置。最后,提供了完整的C++代码实现,展示了如何读取迷宫布局并找到逃脱所需的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目来源:

POJ-2251 Dungeon Master

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 L L, R R R and C C C (all limited to 30 in size).
L L L is the number of levels making up the dungeon.
R R R and C C C are the number of rows and columns making up the plan of each level.
Then there will follow L L L blocks of R R R lines each containing C C 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 L L, R R R and C C 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!

题意:

有一个三维空间的迷宫,从起点出发,找到逃出去的路,如果能逃出去,则输出最短路径的时间,不能则输出Trapped!

view code:

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<ctime>
#define INF 0x3f3f3f3f
#define maxn  10005
using namespace std;
typedef long long LL;

char map[33][33][33];
typedef struct node{
	int x, y, z;
	int step;
}node;

int l,w,h;
int vis[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};// 方向
bool dis[33][33][33]; //标记走过的点

int bfs(node n){
	memset(dis, 0, sizeof(dis));
	queue<node> q;
	n.step = 0;
	node d;
	q.push(n);
	while(!q.empty()){
		n = q.front();
		q.pop();
		for(int i=0; i<6; i++){
			int x = n.x+vis[i][0];
			int y = n.y+vis[i][1];
			int z = n.z+vis[i][2];
			if(x>=h || x<0 || y>=w || y<0 || z>=l || z<0 || map[x][y][z] == '#' || dis[x][y][z])
				continue;
			if(map[x][y][z] == 'E') return n.step+1;
			dis[x][y][z] = 1;
			d = {x,y,z,n.step+1};
			q.push(d);
		}
	}
	return -1;
}

int main(){
	while(scanf("%d%d%d",&h,&w,&l)&&(l||w||h)){
		bool flag = false;
		node start;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				scanf("%s",map[i][j]);
				if(!flag){
					for(int k=0; k<l; k++){
						if(map[i][j][k] == 'S'){//找到起点
							flag = true;
							start.x = i;
							start.y = j;
							start.z = k;
						}
					}
				}
			}
		}
		int time = bfs(start);
		if(time == -1){
			printf("Trapped!\n");
		}
		else printf("Escaped in %d minute(s).\n",time);
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值