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

  本题大意:输入 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. cookie、localStorage、sessionStorage和会话控制机制

    简介 cookie cookie的内容主要包括:名字Name.值Value.域Domain.路径Path.过期时间Expires/Max-Age.大小Size.HTTP.Secure.SameSite ...

  2. spring boot 整合redis --sea 方式1

    application.properties # REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服 ...

  3. leetcode1017

    这道题目很巧妙,似乎是有些过于巧妙了,属于我未知领域的类型,把原作者的解决方案放这里.(看大家都在给差评,我也顺手给个差评吧--) 参考:https://leetcode.com/problems/c ...

  4. <基础> PHP 字符串操作

    explode — 使用一个字符串分割另一个字符串 array explode ( string $delimiter , string $string [, int $limit ] ) implo ...

  5. APPium-Xpath,swipe练习

    写自动化测试,实现 滚动到 口碑最佳 部分,并且打印出所有 口碑最佳 部分的5个应用名称 # coding:utf-8from appium import webdriverimport time d ...

  6. golang redis集群操作:redis-go-cluster

    背景 感觉redis-cli desktop及其难用,最近用golang做了个redis查询工具,支持单例和集群操作,终于不再卡顿!!! 用到的包 "github.com/garyburd/ ...

  7. 页面中的checkbox多选值获取

    依据name名称获取选中值 var arr=document.getElementsByName("name");arr是一个数组,就是所有checkbox的值:for(i=0;i ...

  8. python 连接 Oracle 乱码问题(cx_Oracle)

    用python连接Oracle是总是乱码,最后发现时oracle客户端的字符编码设置不对. 编写的python脚本中需要加入如下几句: import os os.environ['NLS_LANG'] ...

  9. css:margin和padding的百分之使用

    #app { position: fixed; width: 94%; height: 100%; background: pink; padding: 0px 3% 0px 3%;} 如上代码,最终 ...

  10. JAVA_连接池、DataSource、JNDI

    1.连接池    Connection的取得和开放是代价比较高的处理,解决这个问题的方法是连接池.    连接池就是事先取得一定数量的Connection,程序执行处理的时候不是新建Connectio ...