做题时需要注意,爬楼有向上和向下爬之分...

  本题大意:输入 l, r, c, 分别代表地牢的楼层数和每层地牢的长和宽,地牢由rock and point and source and key组成,你初始在s位置,你只能向身边的四个方向和上下方向移动,问你是否能走出地牢,能的话求出最短路径。

  本题思路:BFS爆搜就OK,我不喜欢存运动方向,所以用循环判断了,按道理循环较慢...所以根据读者喜好选择...

  参考代码:

 #include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std; struct node {
int x, y, z, step;
} now, Next, source, key;
const int maxn = + ;
int l, r, c, ans;
char dungeon[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn]; int bfs() {
memset(vis, false, sizeof vis);
source.step = ;
vis[source.x][source.y][source.z] = true;
queue <node> Q;
Q.push(source);
while(!Q.empty()) {
now = Q.front();
Q.pop();
if(now.x == key.x && now.y == key.y && now.z == key.z) return now.step;
Next.step = now.step + ;
for(int i = -; i <= ; i ++)
if(i != ) {
Next.x = now.x + i, Next.y = now.y, Next.z = now.z;
if(Next.x >= && Next.y >= && Next.z >= && Next.x < l && Next.y < r && Next.z < c && !vis[Next.x][Next.y][Next.z] && dungeon[Next.x][Next.y][Next.z] != '#' ) {
Next.step = now.step + ;
vis[Next.x][Next.y][Next.z] = true;
Q.push(Next);
}
}
for(int dy = -; dy <= ; dy ++) {
for(int dz = -; dz <= ; dz ++) {
if((int)(abs(dy - dz)) == ) {
Next.x = now.x + , Next.y = now.y + dy, Next.z = now.z + dz;
if(Next.x >= && Next.y >= && Next.z >= && Next.x < l && Next.y < r && Next.z < c && !vis[Next.x][Next.y][Next.z] && dungeon[Next.x][Next.y][Next.z] != '#') {
vis[Next.x][Next.y][Next.z] = true;
Q.push(Next);
}
}
}
}
}
return -;
} int main () {
while(~scanf("%d %d %d", &l, &r, &c)) {
if(l == && r == && c == ) break;
for(int i = ; i < l; i ++) {
for(int j = ; j < r; j ++) {
for(int k = ; k < c; k ++) {
cin >> dungeon[i][j][k];
if(dungeon[i][j][k] == 'S') { source.x = i; source.y = j; source.z = k; }
if(dungeon[i][j][k] == 'E') { key.x = i; key.y = j; key.z = k; }
}
}
}
ans = bfs();
if(ans != -) printf("Escaped in %d minute(s).\n", ans);
else printf("Trapped!\n");
}
return ;
}

POJ-2251.DungeonMaster(三维BFS)的更多相关文章

  1. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  2. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  3. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  4. Dungeon Master (POJ - 2251)(BFS)

    转载请注明出处: 作者:Mercury_Lc 地址:https://blog.csdn.net/Mercury_Lc/article/details/82693907 题目链接 题解:三维的bfs,一 ...

  5. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  6. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  7. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  8. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  9. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

随机推荐

  1. springMVC数据模型model,modelmap,map,@ModelAttribute的相互关系

    结论: a.注解方法中形参为model,modelmap,map一个或几个时,他们指向的引用对象相同即他们的值相同. b.当使用@ModelAttribute注解请求参数时,springmvc自动将该 ...

  2. Ubuntu 下安装 Swoole

    环境:Ubuntu16.04 apt-get update apa-get install apache2 php php-pear php-dev mysql-server gcc apache2 ...

  3. delphi 属性编辑器

    RegisterPropertyEditor TPictureEditor = class(TClassProperty)   RegisterPropertyEditor(TypeInfo(TPic ...

  4. Spring和SpringMVC的常用注解

    Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...

  5. as2 连接服务器 post

    import mx.utils.Delegate; //接收服务器数据的文本加载器 var result_lv:LoadVars; /** * 数据提交成功后 * 获取的数据 * @param suc ...

  6. netty为啥要二次开发

    很早之前就看过李林峰写的netty的书,但是感觉没有直接用到还是理解不够深入,现在的公司有两套自己基于Netty开发的系统,感觉才真正理解为啥要这么做 借用别人文章回顾下 https://www.cn ...

  7. Java学习路线(转)

    原文:http://www.hollischuang.com/archives/489 一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http ...

  8. 【377】only one element in a tuple

    Recently I am doing the assignment of COMP9021. It is too difficult and it is about the Knight and K ...

  9. 强制停止ORACLE数据库

    操作环境 SuSE+Oracle11gR2 适用场景 shutdown immediate停止数据库失败 操作命令 1.kill掉oracle实例相关进程 2.清除oracle占用的共享内存段 ipc ...

  10. .NETMVC小笔记

    .NETMVC如何不引用_Layout.chtml view文件夹里面有个_ViewStart.cshtml文件,打开可以看到 @{ Layout = "~/Views/Shared/_La ...