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

  本题大意:输入 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. redis 做默认缓存

    配置: server.port= # REDIS (RedisProperties) # Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u ...

  2. EasyExcel 解析excel

    参考:https://blog.csdn.net/jiangjiandecsd/article/details/81115622 https://blog.csdn.net/jianggujin/ar ...

  3. Swoole 结合TP5创建http服务

    下载TP5框架,在项目根目录下创建server目录 http_service.php <?php //创建服务 $http = new swoole_http_server("0.0. ...

  4. [uwsgi: command not found]

    问题: pip install uwsgi 之后,运行uwsgi 报错:[uwsgi: command not found] 解决方案:建立软链接 ln -s /usr/local/python3/b ...

  5. +load +initialize

    +load方法 在app启动的时候各个类的+load方法都会被调用,+load方法不是通过消息机制调用的,它是直接调用的,因此无论是在子类或者category中复写此方法,复写的+load方法都会被调 ...

  6. UNITY2018 真机开启deepprofiling的操作

    手机上运行游戏并开启deepprofiling的命令如下 命令一:adb shell am start -n com.szyh.YHP1.kaopu/com.szyh.YHP1.kaopu.MainA ...

  7. 使用arguments对象验证函数的参数是否合法

    <script>function sum(arg1,arg2) //加法函数{ var realArgCount = arguments.length; //调用函数时传递的实参个数 va ...

  8. Celery + RabbitMq 示意图

    一直搞不清楚消息队列和任务队列是如何结合的,直到碰到了 :http://www.cnblogs.com/jijizhazha/p/8086119.html 中的图,恍然大悟,凭借自己的理解,画了这幅组 ...

  9. 第七次Scrum冲刺

    第七次Scrum冲刺 1.今日完成的任务 队员 今日完成任务 刘佳 前端与后端对接 李佳 后端与数据库对接 周世元 数据库与后端对接 杨小妮 博客编写 许燕婷 管理团队当日及次日任务 陈水莲 综合测试 ...

  10. C++ MFC 改变控件大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...