原题链接

思路:

正常的思路,只不过是将二维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. pnpm 管理依赖包是如何节省磁盘空间的?

    npm 存在的问题 我们经常使用 npm 来管理 node 项目中的包,从 package.json 中读取配置将依赖下载到本地,以保障项目的正常运行. 当项目数量多时,这样的包管理方式会非常的占用电 ...

  2. 【web实验报告】实验二

    一.实验目的 通过一个小型网站的开发,掌握JSP基础知识,加深对session,request,response,cookie等对象的理解,掌握其使用方法,进一步深入掌握HTML.CSS和JavaSc ...

  3. 【Javaweb】做一个房产信息管理系统一

    2019级<JAVA语言程序设计>     上机考试试题                                  2020.12.20     考试要求   一.本试卷为2019 ...

  4. 基于DotNetty实现一个接口自动发布工具 - 通信实现

    基于 DotNetty 实现通信 DotNetty : 是微软的 Azure 团队,使用 C#实现的 Netty 的版本发布.是.NET 平台的优秀网络库. 项目介绍 OpenDeploy.Commu ...

  5. .NET企业应用安全开发动向-概览

    太长不读版:试图从安全的全局视角触发,探讨安全的重要性,讨论如何识别安全问题的方法,介绍.NET提供的与安全相关的基础设施,以及一些与时俱进的安全问题,为读者建立体系化的安全思考框架. 引言 关于&q ...

  6. Head First Java学习:第九章-构造器和垃圾收集器

    对象的前世今生 对象如何创建.存在何处以及如何让保存和抛弃更有效率. 会述及堆.栈.范围.构造器.超级构造器.空引用等. 1.内存的两个区域:堆和栈 堆(heap):对象的生存空间,又称为可垃圾回收的 ...

  7. ProtocolBuffer详细教程

    下面大佬写的特别详细,直接参考他的吧! 推荐参考大佬写的ProtocolBuffer详细教程

  8. 组合式api-ref引用子组件、dom元素, defineExpose的使用

    和vue2一样,我们有时候希望父组件能够调用子组件中的方法.属性.那么就要用到ref. 然后你会发现,根本调用不了子组件中的方法"sonSayHi",如下图: 原因: 使用

  9. Kernel Memory 入门系列:生成并获取文档摘要

    Kernel Memory 入门系列:生成并获取文档摘要 前面在RAG和文档预处理的流程中,我们得到一个解决方案,可以让用户直接获取最终的问题答案. 但是实际的业务场景中,仍然存在一些基础的场景,不需 ...

  10. Android学习--ListView和Tab

    产生一个ListView 其中包含很多items,第一个item启动另一个实现了Tab的Activity. 关于tab的使用方式,参见下面blog http://oldshark.blog.163.c ...