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了..果然还是应当跟着学长混.. 图两边的点分 ...
随机推荐
- BI软件是什么?应用BI工具能给企业带来什么
BI软件是指利用数据挖掘.分析和可视化等技术,将企业内部和外部数据转化为有价值的信息和洞察,以帮助企业支持业务决策和优化业务流程的工具和应用程序.常见的BI软件包括Datainside.QlikVie ...
- AtCoder_abc326
T1 2UP3DOWN 简单的if判断,做题一分钟,翻译十分钟... 代码: #include<bits/stdc++.h> using namespace std; int main() ...
- c# 高并发必备技巧(三)
前面两篇文章主要是介绍了如何解决高并发情况下资源争夺的问题.但是现实的应用场景中除了要解决资源争夺问题,高并发的情况还需要解决更多问题,比如快速处理业务数据等, 本篇文章简要罗列一下与之相关的更多技术 ...
- vue-test4 -----插槽
<template> <!-- <Main class="cccc"/> <component-a/> --> <slot-d ...
- zookeeper JavaApi 创建节点
import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import ...
- Bert-vits2-v2.2新版本本地训练推理整合包(原神八重神子英文模型miko)
近日,Bert-vits2-v2.2如约更新,该新版本v2.2主要把Emotion 模型换用CLAP多模态模型,推理支持输入text prompt提示词和audio prompt提示语音来进行引导风格 ...
- 一文讲透消息队列RocketMQ实现消费幂等
这篇文章,我们聊聊消息队列中非常重要的最佳实践之一:消费幂等. 1 基础概念 消费幂等是指:当出现 RocketMQ 消费者对某条消息重复消费的情况时,重复消费的结果与消费一次的结果是相同的,并且多次 ...
- Scrapy如何在启动时向爬虫传递参数
高级方法: 一般方法: 运行爬虫时使用-a传递参数 scrapy crawl 爬虫名 -a key=values 然后在爬虫类的__init__魔法方法中获取kwargs class Bang123S ...
- Python 实现 PDF 到 Word 文档的高效转换(DOC、DOCX)
PDF(Portable Document Format)已成为一种广泛使用的电子文档格式.PDF的主要优势是跨平台,可以在不同设备上呈现一致的外观.然而,当我们需要对文件内容进行编辑或修改,直接编辑 ...
- 春秋云镜 - CVE-2022-28512
Fantastic Blog (CMS)是一个绝对出色的博客/文章网络内容管理系统.它使您可以轻松地管理您的网站或博客,它为您提供了广泛的功能来定制您的博客以满足您的需求.它具有强大的功能,您无需接触 ...