博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BFS解决POJ 2251
阅读量:4322 次
发布时间:2019-06-06

本文共 2952 字,大约阅读时间需要 9 分钟。

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
 
此题目是三维的迷宫问题,要想解决本题,首先要有二维的BFS相关知识,
有了这个之后,还是不行的,还要搞清楚行走的坐标的表示。
一开始,我本以为要开一个三维的数组来表示走法,但是,用二维的才是正确的。
如何正确,我将在代码中做出详细的说明。另外的一些注意事项,也会在代码中
做出详细的解释。
View Code
#include
#include
using namespace std;int level, row, col, i, j, k, sx, sy, sz, tx, ty, tz;char map[35][35][35];int used[35][35][35];int step[35][35][35];int direction[6][3] = {
{
0,0,1},{
0,0,-1},{
0,1,0},{
0,-1,0},{
1,0,0},{-1,0,0}};///好了,现在来好好解释一下direction[][]数组,也就是走法。一共有6种走法,上下左右前后。故要开direction[6][];///为何要开direction[][3]呢?也就是这个3该如何解释呢?学过几何的人都知道x,y,z的空间坐标轴。这就是3的来历。///拿{0,0,1}来说,表示x=0, y=0, z=1,很明显了,这种走法表示的是向上走了。其它的类似,不再说了。///同样的,direction[4][2]中的2,也是同样的道理。只不过没有了z坐标。struct point{ int x,y,z;}queue[30000];int BFS(int sz, int sx, int sy){ used[sz][sx][sy] = 1; int head = 0, tail = 0; queue[head].z = sz; queue[head].x = sx; queue[head].y = sy; head++; while(tail
=0 && t2.z
=0 && t2.x
=0 && t2.y>level>>row>>col && (level+row+col)) { memset(used, 0, sizeof(used)); memset(step, 0, sizeof(step)); for(i=0; i
>map[i][j][k]; if(map[i][j][k]=='S') ///一定要注意这里的map[i][j][k]中的三个参数i, j, k. /// i 对应层数,也就是z轴 /// j 对应行数,也就是x轴 /// z 对应列数,也就是y轴 ///因此,在下面的运算中要一一对应起来的。否则将会出错的。 { sx = j; sy = k; sz = i; } } int result = BFS(sz, sx, sy); if( result==-1 ) cout<<"Trapped!"<
 

 

 
 

转载于:https://www.cnblogs.com/o8le/archive/2011/09/23/2186741.html

你可能感兴趣的文章
eclipse + maven + scala+spark环境搭建
查看>>
jmeter中webdriver插件,进行自动化压测
查看>>
整站开发初始化
查看>>
洛谷P2900 [USACO08MAR]土地征用Land Acquisition(斜率优化)
查看>>
uoj#448. 【集训队作业2018】人类的本质(Min_25筛+拉格朗日插值)
查看>>
vim配置及插件安装管理(超级详细)
查看>>
楼市仅是阶段性回暖 去库存仍是明年楼市主基调
查看>>
UIImagePickerController
查看>>
怎样打开64位 Ubuntu 的32位支持功能?
查看>>
关于docker jenkins启动时失败的问题处理
查看>>
JavaScript 循环绑定之变量污染
查看>>
poj 1038 Bugs Integrated, Inc. 三进制状态压缩 DFS 滚动数组
查看>>
zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
查看>>
Wordpress解析系列之PHP编写hook钩子原理简单实例
查看>>
怎样看待个体经济
查看>>
不明觉厉的数据结构题2
查看>>
面向对象编程思想概览(四)多线程
查看>>
二十三种设计模式及其python实现
查看>>
Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类...
查看>>
【设计模式】 访问者模式
查看>>