原题链接

思路:

正常的思路,只不过是将二维BFS换成三维的,也算是个模板题吧(PS:DFS超容易超时)

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char chart[40][40][40]; int then[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1} };
int l, r, c;
int el, er, ec, sl, sr, sc;
int ans;
struct node
{
int step;
int l;
int r;
int c;
};
bool judge(int x, int y, int z)
{
if (x >= 0 && x < l && y >= 0 && y < r && z >= 0 && z < c && chart[x][y][z] != '#')
return true;
else
return false;
}
void bfs()
{
int flag = 0;
queue<node> q;
node t;
t.step = 0;
t.l = sl;
t.r = sr;
t.c = sc;
q.push(t);
chart[sl][sr][sc] = '#';
while (!q.empty())
{
int x, y, z;
x = q.front().l;
y = q.front().r;
z = q.front().c;
for (int i = 0; i < 6; i++)
{
int xx = x + then[i][0];
int yy = y + then[i][1];
int zz = z + then[i][2];
if (judge(xx, yy, zz))
{
if (xx == el && yy == er && zz == ec)//在找到的时候就退出,如果在走到的时候再退出的话,会t
{
ans = q.front().step + 1;
return;
}
node temp;
temp.step = q.front().step + 1;
temp.l = xx;
temp.r = yy;
temp.c = zz;
chart[xx][yy][zz] = '#';
q.push(temp);
}
}
q.pop();
}
ans = -1;
}
int main()
{
while (scanf("%d%d%d", &l, &r, &c))
{
if (l == 0 && r == 0 && c == 0)
break; for (int i = 0; i < l; i++)
{
for (int j = 0; j < r; j++)
{
scanf("%s", chart[i][j]);
for (int k = 0; k < c; k++)
{
if (chart[i][j][k] == 'S')
{
sl = i;
sr = j;
sc = k;
}
if (chart[i][j][k] == 'E')
{
el = i;
er = j;
ec = k;
}
}
}
}
bfs();
if (ans == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
} return 0;
}

POJ:Dungeon Master(BFS模板题)的更多相关文章

  1. [poj] Dungeon Master bfs

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

  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. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

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

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

  5. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. POJ 2251 三维BFS(基础题)

    Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...

  7. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  8. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  9. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  10. POJ 3041 匈牙利算法模板题

    一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...

随机推荐

  1. 音色逼真、韵律自然的AI人声克隆限时福利!

    声音,为数字人注入灵魂. 2023云栖大会上,阿里云视频云接受了CCTV-2财经频道的采访,分享并演示了如何利用云端智能剪辑,一站式完成数字人渲染及视频精编二创. 正如视频开头所呈现的AI重现演员&q ...

  2. day01预习-基本语法

    typora-copy-images-to: media 基本语法 JavaScript的历史: ​ 在95年以前,就有很多上网的用户了,当时的带宽只有28.8kb/s,用户要进行表单的验证时,点击提 ...

  3. [python] 基于Tablib库处理表格数据

    Tablib是一个用于处理电子表格(如 Excel,CSV,JSON)的Python 库.它提供了一种简单而强大的方式来操作和处理数据.利用Tablib,我们可以轻松地读取.写入.过滤和转换各种类型的 ...

  4. Educational Codeforces Round 56 (Rated for Div. 2) G题(线段树,曼哈顿距离)

    题目传送门 以二维为例,二维下两点间的曼哈顿距离最大值为\(max(|x_i-x_j| + |y_i-y_j|)\),可以通过枚举坐标符号正负来去掉绝对值.即\(max(x_i-x_j+y_i-y_j ...

  5. C与Verilog差别

    C没有时钟概念,Verilog有时钟边沿触发. C无建立保持时间要求,Verilog要计算建立保持时间,并进行优化 C与工艺无关,Verilog依赖底层工艺cell,相同代码不同cell差异较大. V ...

  6. [ABC280G] Do Use Hexagon Grid 2

    Problem Statement A hexagonal cell is represented as $(i,j)$ with two integers $i$ and $j$. Cell $(i ...

  7. IDEA创建MyBatis项目--实现简单的查操作

    IDEA创建MyBatis项目--实现简单的查操作 1.创建一个maven工程,不使用模板 2.通过maven加载Mybatis依赖包 在pom文件中导入maven坐标 <dependencie ...

  8. Python——第四章:迭代器(Iterators)

    迭代器iterator: 提到迭代器,最典型的就是for循环 for 变量 in 可迭代: pass 可迭代对象iterable: 是指可以使用 for 循环进行遍历的对象.除了字符串 (str).列 ...

  9. 新版的Django中的path不能使用正则表达式

    新版的path 虽然 取代了 之前的url,但是在写路由的时候不能在路由中直接写正则表达式,不然会找不到页面. 解决方法使用 re_path from django.urls import re_pa ...

  10. <Python全景系列-1> Hello World,1分钟配置好你的python环境

    <从此开始:1分钟配置好你的python环境> 欢迎来到我们的系列博客<Python360全景>!在这个系列中,我们将带领你从Python的基础知识开始,一步步深入到高级话题, ...