博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
暑期第一弹<搜索> B - Dungeon Master(三维BFS,6个状态)
阅读量:6903 次
发布时间:2019-06-27

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

B - Dungeon Master
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
 

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!

题意:有一个三维的空间,共l层,每层r行n列,#表示墙壁;有一头龙在S处,每一次他可以上下,前后,左右,六个方向移动。问它到达E点的最短步数。

思路:最典型的bfs求最短路问题了,可以套着板子做了都,每次有6个状态,分别将6个状态加入队列就好。切记不可忘了边界判断条件。 理解bfs可以用扇形区域来理解,每次都一层一层向外扩散,像扇子一样,先出队列且为目标位置的点一定是最短路径到达的。
再说一遍,利用树理解!
代码如下:

#include 
#include
#include
#include
using namespace std;struct Node{ int x,y,z; int cont;};char Map[35][35][35];int visit[35][35][35];int dir[6][3] = {
{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};int l,r,c;int x1,y1,z1;void bfs(){ queue
Q; Node t; t.x = x1,t.y = y1,t.z = z1,t.cont = 0; visit[x1][y1][z1] = 1; Q.push(t); while(!Q.empty()){ Node res = Q.front(); Q.pop(); int xx = res.x; int yy = res.y; int zz = res.z; int Cont = res.cont; if(Map[xx][yy][zz] == 'E'){ //目标位置且一定最短 cout<<"Escaped in "<
<<" minute(s)."<
< 6;i ++){ //每一步6个状态 Node temp; int xi = temp.x = xx+dir[i][0]; int yi = temp.y = yy+dir[i][1]; int zi = temp.z = zz+dir[i][2]; temp.cont = Cont+1; if(xi < 1 || xi > l || yi < 1 || yi > r || zi < 1 || zi > c) continue; //边界判断 if(Map[xi][yi][zi] != '#' && !visit[xi][yi][zi]){ visit[xi][yi][zi] = 1; Q.push(temp); } } } cout<<"Trapped!"<
>l>>r>>c){ if(l == 0 && r == 0 && c == 0) break; for(int i = 1;i <= l;i ++){ for(int j = 1;j <= r;j ++){ for(int k = 1;k <= c;k ++){ cin>>Map[i][j][k]; if(Map[i][j][k] == 'S') x1 = i,y1 = j,z1 = k; } } } memset(visit,0,sizeof(visit)); bfs(); } return 0;}

转载于:https://www.cnblogs.com/Jstyle-continue/p/6351942.html

你可能感兴趣的文章
vs设置异常就断下
查看>>
win7 共享打印机后,客户端连接提示:打印机已删除(0x00000709)
查看>>
工作与生活之平衡(4)微博病患者
查看>>
Andriod第七课-----数据库
查看>>
Shell使用for循环语句
查看>>
ASP.NET设计的几个技巧
查看>>
电脑爱好者GHOSTWIN7纯净版V1.0
查看>>
Bootstrap3系列:输入框组
查看>>
刘启成_第七章实验(四):case
查看>>
APUE读书笔记-02UNIX标准和实现-03UNIX实现
查看>>
aFleX案例:使用同一公网IP管理内部多台服务器
查看>>
我的友情链接
查看>>
Linux常用命令之rmdir
查看>>
新反向代理与负载均衡工具 traefik 安装配置部署详解
查看>>
面试题目集锦-bash篇
查看>>
我的友情链接
查看>>
Seliux安全配置
查看>>
Vmware vSphere 6.0之安装 vCenter Server Appliance
查看>>
GlusterFS客户端进程分析
查看>>
kvm宿主机物理内存预留方案
查看>>