NOI 2.5基本算法之搜索 三维迷宫问题----分析

一、题目描述

总时间限制:

1000ms
内存限制:
65536kB
描述
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!

题目大意:一道三维迷宫题目,长宽高<=30,样例输入中的数字(高 长 宽),如果输入“0 0 0”,程序结束。

“S”为入口,“E”为出口,“#”为墙,每移动一格(上下,左右,前后)都要一分钟,

输出最短需要的时间,“Escaped in 时间 minute(s).”

如果不能,则输出“Trapped!”。

 

二、分析

因为要求出最短的时间,一定要用广搜,就跟走出迷宫差不多,

建立四个队列s1,s2,s3,sum(可以把s1,s2,s3,合并成一个结构体队列)。

#include<cstdio>
#include<queue>
using namespace std;
int n,m,l,e,r,t,aa;
int sx[6]={-1,1,0,0,0,0},zy[6]={0,0,-1,1,0,0},qh[6]={0,0,0,0,-1,1};//方向数组
char a[35][35][35];
bool f;

然后再开始写广搜,

广搜代码如下

void bfs()
{
	f=0;
	int k1,k2,k3;
	while(!s1.empty())
	{
		for(int i=0;i<=5;i++)
		{
			if(s1.back()==e&&s2.back()==r&&s3.back()==t){
				f=1;
				break;
			}
			k1=s1.front()+sx[i];k2=s2.front()+zy[i];k3=s3.front()+qh[i];
			if(k1>=1&&k1<=n&&k2>=1&&k2<=m&&k3>=1&&k3<=l&&a[k1][k2][k3]!='#'){
				s1.push(k1);
				s2.push(k2);
				s3.push(k3);
				sum.push(sum.front()+1);
				a[k1][k2][k3]='#';
			}
		}
		if(f==1)
			break;
		s1.pop();
		s2.pop();
		s3.pop();
		sum.pop();
	}
	if(f==1)
		aa=sum.back();
	if(f==0)
		aa=0;
	return;
}

 

主程序的代码很简单,就是进行初始化和输入输出。

但是要注意,在每次循环之前要把队列pop()清空。

int i,j,k,q,w,qw;
	while(scanf("%d %d %d\n",&n,&m,&l)==3){
		if(n==0&&m==0&&l==0)
			break;
		while(!s1.empty())
			s1.pop();
		while(!s2.empty())
			s2.pop();
		while(!s3.empty())
			s3.pop();
		while(!sum.empty())
			sum.pop();
		for(i=1;i<=n;i++){
			//输入,找起点和终点
		}
		a[q][w][qw]='#';
		//元素入队
		aa=0;
		bfs();
		if(aa!=0)
			printf("Escaped in %d minute(s).\n",aa);
		if(aa==0)
			printf("Trapped!\n");
	}

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/c20180602_csq/article/details/52525164
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞