首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2251 - Dungeon Master(dfs + 队列)

【POJ】2251 - Dungeon Master(dfs + 队列)

作者头像
FishWang
发布2025-08-26 19:57:35
发布2025-08-26 19:57:35
8800
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Dungeon Master

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 26403

Accepted: 10271

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

代码语言:javascript
代码运行次数:0
运行
复制
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
Escaped in 11 minute(s).
Trapped!

Source

Ulm Local 1997

还是bfs加队列。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
char mapp[35][35][35];
int a,b,c;		//地图总大小 
int st_x,st_y,st_z,endd_x,endd_y,endd_z;
int move_x[6] = {0,0,0,0,1,-1};
int move_y[6] = {0,0,1,-1,0,0};
int move_z[6] = {1,-1,0,0,0,0};
bool used[35][35][35];
struct node
{
	int x,y,z;
	int step;
}pr,ne;

bool check(node t)
{
	if (t.x < 1 || t.x > a || t.y < 1 || t.y > b || t.z < 1 || t.z > c || used[t.x][t.y][t.z] || mapp[t.x][t.y][t.z] == '#')
		return false;		//不能移动
	return true; 
}
int bfs()
{
	queue<node> q;
	CLR(used,0);
	pr.x = st_x;
	pr.y = st_y;
	pr.z = st_z;
	pr.step = 0;
	q.push(pr);
	used[pr.x][pr.y][pr.z] = true;
	while (!q.empty())
	{
		pr = q.front();
		q.pop();
		for (int i = 0 ; i < 6 ; i++)
		{
			ne = pr;
			ne.x += move_x[i];
			ne.y += move_y[i];
			ne.z += move_z[i];
			ne.step++;
			if (!check(ne))
				continue;
			if (ne.x == endd_x && ne.y == endd_y && ne.z == endd_z) 
				return ne.step;
			used[ne.x][ne.y][ne.z] = true;		//在这里标记而不是外面取数的时候标记 
			q.push(ne);
		}
	}
	return -1;
}
int main()
{
	while (~scanf ("%d %d %d",&a,&b,&c) && (a || b || c))
	{
		for (int i = 1 ; i <= a ; i++)
		{
			for (int j = 1 ; j <= b ; j++)
			{
				scanf ("%s",mapp[i][j]+1);
				for (int k = 1 ; k <= c ; k++)
				{
					if (mapp[i][j][k] == 'S')
					{
						st_x = i;
						st_y = j;
						st_z = k;
					}
					else if (mapp[i][j][k] == 'E')
					{
						endd_x = i;
						endd_y = j;
						endd_z = k;
					}
				}
			}
		}
		int ans = bfs();
		if (ans == -1)
			printf ("Trapped!\n");
		else
			printf ("Escaped in %d minute(s).\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档