ZOJ1940 Dungeon Master 广度优先

该文描述了一个3D迷宫问题,要求找到从起点到终点的最短路径。利用广度优先搜索(BFS)策略,通过建立三维数组存储迷宫信息,并进行遍历,寻找最短步数。当找到出口或遍历完所有可能路径而未找到出口时,分别输出最短时间和“Trapped!”。
摘要由CSDN通过智能技术生成

在读大学生一枚,感谢每一个你的点击阅读! !

问题描述

链接指路🔗

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!

Code Size Limit 32 KB
Time Limit 2000 ms
Memory Limit 64 MB

省流:给定一个立体迷宫,求出从起点到终点的最短路径所对应的时间,若起点没有任何路径可以到达重点,输出“Trapped!”。

解题思路

1. 问题定性

本题属于求解最短路径问题,与二维迷宫问题类似,优先考虑广度优先

2. 数据存储

  • 毋庸置疑地,迷宫数据应当存起来。
  • 由于迷宫是三维的,故建立一个三维数组maze来存储迷宫数据。
  • 应当注意,maze的第一维度表示层数level,第二维度表示行row,以及第三维度表示列col。
  • 在输入maze的同时,获取起点和终点的坐标。

3. 核心算法

  • 将起点push进队列,接着进入一个while循环。
  • 当队列不空时,用temp记录队列front结点,并把该结点pop出队列,判断该结点是不是终点。
    • 是:按格式输出最短时间,后return,函数调用结束
    • 不是:将是‘.’的邻接点push进队列,步数move需在该结点的基础上加1。
  • 队列为空时,此时while循环结束,表明起点到终点没有路径,因此输出"Trapped!"。

4. 两个细节

  • 由于终点在maze数组中以‘E’的形式存储,因此在获取终点坐标的同时,应当把maze中终点位置的元素改为‘.’,这样终点才可能被push进队列中。
  • 将邻接点push进队列之后,需要把maze中对应元素标记为非‘.’的元素,个人采用标记‘#’的形式,通过这样可以避免结点重复进队的问题。

实现代码

#include <iostream>
using namespace std;
#include <queue>
struct node{
	int level;
	int row;
	int col;
	int move;
};
node beginn,endd;
int L,R,C;

char maze[35][35][35];
int nextVisit[6][3]={{-1,0,0},{1,0,0},{0,0,-1},{0,0,1},{0,1,0},{0,-1,0}};

void bfs()
{
	queue<node> q;
	q.push(beginn);
	
	node temp;
	while(!q.empty())
	{
		temp=q.front();
		q.pop();
		
		if(temp.level==endd.level&&temp.row==endd.row&&temp.col==endd.col)
		{
			cout<<"Escaped in "<<temp.move<<" minute(s)."<<endl;
			return ;
		}
		
		int i;
		for(i=0;i<6;i++)
		{
			int ll=temp.level+nextVisit[i][0];
			int rr=temp.row+nextVisit[i][1];
			int cc=temp.col+nextVisit[i][2];
			if(ll>=0&&ll<L&&rr>=0&&rr<R&&cc>=0&&cc<C)
			{
				if(maze[ll][rr][cc]=='.') 
				{
					node tempNode;
					tempNode.level=ll;
					tempNode.row=rr;
					tempNode.col=cc;
					tempNode.move=temp.move+1;
					q.push(tempNode);
					maze[ll][rr][cc]='#';     //标记为'#' 即已进队列  防止重复进队 因为后进队的步数肯定更多 
				}
			}
		}
		
	}
	
	cout<<"Trapped!"<<endl;
	
}

int main()
{
	while(cin>>L>>R>>C&&L!=0)
	{
		int i,j,k;
		for(i=0;i<L;i++)
		{
			for(j=0;j<R;j++)
			{
				for(k=0;k<C;k++)
				{
					cin>>maze[i][j][k];
					if(maze[i][j][k]=='S')
					{
						beginn.level=i;
						beginn.row=j;
						beginn.col=k;
						beginn.move=0;
						maze[i][j][k]='#';
					}
					else if(maze[i][j][k]=='E')
					{
						endd.level=i;
						endd.row=j;
						endd.col=k;
						maze[i][j][k]='.';
					}
				}
			}
		} 
		
		bfs();
	}
	
	return 0;
} 

如果有任何问题,欢迎在评论区或者私信中指出,一起进步,感谢大家! !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值