3Ddungeon ~ 3D迷宫 BFS

这是一篇关于如何解决3D迷宫问题的文章,通过使用宽度优先搜索(BFS)策略来寻找从起点'S'到终点'E'的最短路径。题目描述了一种情况,你在由'#'表示的岩石和'.'表示的空地构成的3D地牢中,只能沿着六个基本方向移动,不能斜向移动,并且必须在限定时间内找到最快的出路。如果可以逃脱,输出以分钟为单位的最短时间;如果无法逃脱,则输出'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? 

输入

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.

输出

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!

样例输入

3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0

样例输出

Escaped in 11 minute(s).
Trapped!

提示

本题的意思其实就是一个3D迷宫,相对与2d 迷宫就多了一个z,

思路bsf

dfs应该会超时;

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct Node
{
	int x , y, z, step;
	Node() {} 
	Node(int xx,int yy,int zz,int ss) : x(xx) , y (yy) , z(zz) , step(ss) {}
};
int sx,sy,sz,ex,ey,ez;
const int dx[6] = { 0,0,1,0,-1,0 };
const int dy[6] = { 0,0,0,1,0,-1 };
const int dz[6] = { -1,1,0,0,0,0 };
char a[35][35][35];
int book[35][35][35];
int n, m ,l ;
int bfs(int x,int y,int z,int step)
{
	queue<Node> Q;
	Q.push(Node(x,y,z,step));
	while(!Q.empty())
	{
		Node u = Q.front() ;
		Q.pop();
		if(u.x == ex && u.y == ey && u.z == ez)
		{
			return u.step ;
		} 
		int nowx,nowy,nowz,nows;
		for(int i = 0 ; i < 6 ; i++)
		{
			nowx = u.x + dx[i] ;
			nowy = u.y + dy[i] ;
			nowz = u.z + dz[i] ;
			nows = u.step + 1 ;
			if(nowx>=0 && nowx < n && nowy >= 0 && nowy < m && nowz >= 0 && nowz < l&&a[nowx][nowy][nowz] != '#' &&book[nowx][nowy][nowz] == 0 )
			{
				book[nowx][nowy][nowz] = 1 ;
			//	a[nowx][nowy][nowz] = '#';
				Q.push(Node(nowx,nowy,nowz,nows)) ; 
				
			}
		}
 	}
 	return -1 ; 
}
int main()
{
	while(cin>>n>>m>>l)
	{
		memset(a,0,sizeof(a));
		memset(book,0,sizeof(book));
		if(n==0 && m==0 && l == 0)
		{
			break;
		}
		for(int i=0 ; i < n ; i++)
		{
			for(int j=0 ; j < m ; j++)
			{
				for(int k=0 ; k < l ; k++)
				{
					cin>>a[i][j][k];
					if(a[i][j][k] == 'S')
					{
						sx=i;
						sy=j;
						sz=k;
					}
					if(a[i][j][k] == 'E')
					{
						ex=i;
						ey=j;
						ez=k;
					}
					/*if(a[i][j][k] == '#')
					{
						book[i][j][k] = 1;
					}*/
					
				}
			}
		}
		book[sx][sy][sz] = 1;
		int ans=bfs(sx,sy,sz,0);
		if(ans==-1)
		{
			cout<<"Trapped!"<<endl;
		}
		else
		{
			printf("Escaped in %d minute(s).",ans);
			cout<<endl;
		}
	} 
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值