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了..果然还是应当跟着学长混.. 图两边的点分 ...
随机推荐
- python数值列表之range()和list()
range() 学习了for循环后,显示数字当然也可以很轻松啦,这个时候我们就可以用到range()函数 for list_2 in range(1, 5): print(list_2) range( ...
- 微信小程序-敏感内容检测 文本过滤 图片检测 https
一.获取access_token wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/token', method : 'GET', data : ...
- 【LOJ NOI Round#2 Day1 T1】单枪匹马(矩阵乘法)
题目传送门 操作二要求的东西是一个循环迭代的东西,手推相邻两项找下规律,发现相邻两项的分子分母间含有线性关系,考虑用矩阵乘法求解.对于 \([1,n]\)的询问,从后往前倒推, \(x_{n-1}=a ...
- git 同时推送多个远程仓库
今天遇到个git的问题:需要同时提交到两个远程仓库 解决方法: git add . git commit -m '提交信息' git remote -v git remote add old_orig ...
- 我最喜欢的白版应用,AI加持的新功能开源!强烈推荐
Excalidraw 把他们的文本到图表的功能开源了 Excalidraw是一个虚拟白板应用,专门用于绘制类似手绘的图表.它提供了一个无限的.基于画布的白板,具有手绘风格,支持多种功能. 之前我分享的 ...
- [ARC168E] Subsegments with Large Sums
题目链接 看到严格选 \(k\) 个,不难想到 WQS二分.定义 \(f(x)\) 为分成 \(x\) 段,最多有多少个超过 \(S\) 的.然后你会发现他不是凸的.因为他有很多平段,比如把两个很小的 ...
- [ABC282F] Union of Two Sets
Problem Statement This is an interactive task, where your and the judge's programs interact via Stan ...
- DRF过滤器
https://www.django-rest-framework.org/api-guide/filtering 一般情况下,我们可以重写DRF视图类的get_queryset()方法来实现查询结果 ...
- ios上架流程 详细通关教程 2021
记录此文是源于以下需求 1.已有app store开发者账号 (公司账号$99),需上架至app store 2.有商城实体商品支付功能(会员等虚拟支付另说) 3.有硬件交互功能 注:建议预留一周上架 ...
- 盘点前端的那些Ajax请求:从ES5到React
说起前端开发,Ajax请求是绕不开的技术点.然而,程序语言更新换代越来越快,Ajax请求的方式也是各有不同. 在使用ES5开发的时候,我们还在使用最原始的XMLHttpRequest对象: // cr ...