POJ:Dungeon Master(BFS模板题)
思路:
正常的思路,只不过是将二维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模板题)的更多相关文章
- [poj] Dungeon Master bfs
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- 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 ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2251 三维BFS(基础题)
Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- hdu1242 又又又是逃离迷宫(bfs模板题)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...
随机推荐
- Linux笔记02: Linux环境_2.2 Linux系统安装
2.2 Linux系统 本文使用的Linux系统为CentOS 7.9.2009,读者可以根据自己的需要选择不同的版本. 2.2.1 CentOS版本 CentOS基本上是安装在i386.x86_64 ...
- 微软官方发布的C#开源、免费、实用的Windows工具箱
前言 今天分享一款由微软官方发布的C#开源.免费.实用的Windows工具箱(帮助用户调整和简化Windows系统的体验,从而提高工作效率):Microsoft PowerToys. 项目介绍 Mic ...
- 使用nginx搭建creates.io镜像(稀疏索引)
在Rust开发中,经常需要使用Cargo从crates.io下载依赖,而国内几乎没有好用的crates.io镜像,大多都只对crates.io-index和crates.io进行了镜像,而最重要的st ...
- 递归产生StackOverflowError
package com.guoba.digui; public class Demo01 { public void A(){ A();//自己调用自己,递归没用好,产生错误java.lang.Sta ...
- 如何在LinkedIn上开发客户
LinkedIn作为一个职场社交平台,提供了许多开发外贸客户的机会和工具.通过在LinkedIn上建立个人和公司的专业形象.分享有价值的内容.参与行业社群和利用广告推广,您可以扩大您的业务网络,找到更 ...
- IDEA插件(1 UI美化)
一.IDEA 插件怎么安装?(图文讲解) IntelliJ IDEA 支持丰富的插件,熟练使用相关插件,能够有效提高我们的开发效率以及用户体验.那么,要如何在 IDEA 中安装插件呢?这里有两种方式: ...
- Socket.D 替代 Http 协议像 Ajax 一样开发前端接口
我们在"前端接口"开发时,使用 socket.d 协议有什么好处: 功能上可以替代 http 和原生 ws 更安全!现有的工具想抓包数据,难!难!难!(socket.d 是个新的二 ...
- Pikachu漏洞靶场 PHP反序列化
PHP反序列化 查看源码,以下为关键代码: class S{ var $test = "pikachu"; function __construct(){ echo $this-& ...
- 文心一言 VS 讯飞星火 VS chatgpt (57)-- 算法导论6.4 1题
文心一言 VS 讯飞星火 VS chatgpt (57)-- 算法导论6.4 1题 一.参照图 6-4 的方法,说明 HEAPSORT 在数组 A=(5,13,2,25,7,17,20,8,4)上的操 ...
- SQLite3使用笔记(1)——查询
目录 1. 概述 2. 详论 2.1. 打开/关闭数据库 2.2. 数据查询 3. 参考 1. 概述 SQLite是一个嵌入式SQL数据库引擎.与大多数其他 SQL 数据库不同,SQLite 没有单独 ...