NOI 2.5基本算法之搜索 三维迷宫问题----分析
2016-09-13 13:41
411 查看
一、题目描述
总时间限制:
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"); }
相关文章推荐
- NOI 2.5基本算法之搜索 走出迷宫----广搜算法的实现
- NOI2.4基本算法之分治 统计数字 分析----换一种思路
- [OpenJudge] 2.5基本算法之搜索 取石子游戏
- 【计算机算法分析】递归与分治策略——二分搜索与集合划分问题
- 1253:Dungeon Master(2.5基本算法之搜索)
- Python排序搜索基本算法之插入排序实例分析
- Python排序搜索基本算法之希尔排序实例分析
- [POJ2386][OpenJudge] 2.5基本算法之搜索 Lake Counting
- 8465:马走日(2.5基本算法之搜索)
- 2727:仙岛求药(2.5基本算法之搜索)
- NOI2.2基本算法之递归和自调用函数 全排列 分析----如何写全排列函数
- [OpenJudge] 2.5基本算法之搜索 仙岛求药
- 1792:迷宫(2.5基本算法之搜索)
- 156:LETTERS(2.5基本算法之搜索)
- Python排序搜索基本算法之选择排序实例分析
- Python排序搜索基本算法之归并排序实例分析
- Python排序搜索基本算法之冒泡排序实例分析
- 1756:八皇后(2.5基本算法之搜索)
- 2.5基本算法之搜索 2727:仙岛求药——广搜
- [OpenJudge] 2.5基本算法之搜索 红与黑