Dungeon Master

本文介绍了一种解决三维迷宫逃脱问题的广搜(BFS)算法,详细阐述了如何通过六方向搜索来找到从起点到终点的最短路径,并提供了具体的代码实现和输入输出示例。

算法:广搜(BFS);

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!

三维的向六个方向搜:a[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};

代码:

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int n,m,k,q1,p1,s1,s2,p2,q2,t,b[35][35][35];
char map[35][35][35];
int a[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};
struct cm
{
	int x,y,z,step;
};
void BFS(int dx,int dy,int dz)
{   memset(b,0,sizeof(b));
	queue<cm>que ;
	cm cur,loer;
	cur.x=dx;
	cur.y=dy;
	cur.z=dz;
	cur.step=0;
	b[dx][dy][dz]=1;
	que.push(cur);
	while(que.size())
	{
		loer=que.front();
		que.pop();
		if(loer.x==s2&&loer.y==p2&&loer.z==q2)
		{
			t=1;
			cout<<"Escaped in "<<loer.step<<" minute(s)."<<endl;
			break;
		}
		for(int i=0;i<6;i++)
		{
			int nx=loer.x+a[i][0];
			int ny=loer.y+a[i][1];
			int nz=loer.z+a[i][2];
			if(nx>=0&&nx<n&&ny>=0&&ny<m&&nz>=0&&nz<k&&map[nx][ny][nz]=='.'&&!b[nx][ny][nz])
			{
				cur.x=nx;
				cur.y=ny;
				cur.z=nz;
				cur.step=loer.step+1;
				b[nx][ny][nz]=1;
				que.push(cur);
			}
		}
	}
}
int main()
{   int i,j,s;
	while(cin>>n>>m>>k&&n&&m&&k)
	{
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{cin>>map[i][j];
				for(t=0;t<k;t++)
				{
					if(map[i][j][t]=='S')
					{
						s1=i;p1=j;q1=t;
						map[i][j][t]='.';
					}
					else if(map[i][j][t]=='E')
					{
						s2=i;p2=j;q2=t;
						map[i][j][t]='.'; 
					}
					
				}
			}
		}
		t=0;
		BFS(s1,p1,q1);
		if(t==0) cout<<"Trapped!"<<endl;
	}
}



转载于:https://www.cnblogs.com/wangyumin/p/5323443.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值