hdu1072(Nightmare)bfs
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5647 Accepted Submission(s): 2808
Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.
Here are some rules: 1. We can assume the labyrinth is a 2 array. 2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too. 3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth. 4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb. 5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish. 6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
这题不同于其他题的地方就是于虽然也是bfs,但对于走过的路径不能标记,因为可能还要走,
注意题目要求:
如果可以,可以走任意多遍。
这就引发了一个问题,如果不缩减搜索范围,怎么可能走得出来呢?应该说这题好就好在不是
根据走过的路径来标记,而是根据前后两次踏上同一位置的时间长短来标记。
简言之,如果第二次踏上一个位置,那么找出路已用的时间肯定是增加了,那为啥还要走上这条
路呢?唯一的追求就是bomb离爆炸的时间增大了。所以可以利用这个条件来标记了。每次在入队
前检查下爆炸时间是否比上次在同一位置的大,若是,则入队;反之,入队无意义了。
ps:http://acm.hdu.edu.cn/showproblem.php?pid=1072
附上我代码:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int dir[][]={{,},{,},{-,},{,-}};
int map[][];
int mark[][];
int i,j,n,m; struct node
{
int x,y;
int time;//记录时间
int step;//记录步数
}s; int check(int x,int y)
{
if(x<||x>=m||y<||y>=n)//判断边界
return ;
else
return ;
} void bfs()
{
queue<node>Q;
Q.push(s);
mark[s.x][s.y]=s.time; node now,next;
while(!Q.empty ())
{
now=Q.front();
Q.pop();
for( i=;i<;i++)
{
next=now;
next.x+=dir[i][];
next.y+=dir[i][];
if(!check(next.x,next.y)||!map[next.x][next.y])
continue;
next.step++;
next.time--;
if(map[next.x][next.y]==)
{
printf("%d\n",next.step);
return ;//结束循环
}
else if(map[next.x][next.y]==)//计时归位
{
next.time=;
}
if(next.time>&&mark[next.x][next.y]<next.time)//标记该点当剩余时间更大时就标记
{
mark[next.x][next.y]=next.time;//标记
Q.push(next);
} }
}
printf("-1\n");//队列为空还没找到就是-1了
}
int main()
{
int num;
scanf("%d",&num);
while(num--)
{
scanf("%d%d",&m,&n);
for(i=;i<m;i++)
for(j=;j<n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==)
{
s.x=i;
s.y=j;
s.time=;
s.step=;
}
mark[i][j]=;
}
bfs();
}
return ;
}
大家相互交流,共同提高哈!
hdu1072(Nightmare)bfs的更多相关文章
- 迷宫寻宝(一)(bfs)
迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编 ...
- python 网络爬虫(二) BFS不断抓URL并放到文件中
上一篇的python 网络爬虫(一) 简单demo 还不能叫爬虫,只能说基础吧,因为它没有自动化抓链接的功能. 本篇追加如下功能: [1]广度优先搜索不断抓URL,直到队列为空 [2]把所有的URL写 ...
- 步步为营(十六)搜索(二)BFS 广度优先搜索
上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
- HDOJ(1242)BFS+优先队列
Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:"#"是墙,"."是路,"a&quo ...
- nyist 82迷宫寻宝(一)(BFS)
题目连接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 此题在基础BFS上加入了门和钥匙,要找齐所有钥匙才能开门,所以要对门特殊处理. 1.先 ...
- LeetCode 5073. 进击的骑士(Java)BFS
题目:5073. 进击的骑士 一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里. 骑士的走法和中国象棋中的马相似, ...
- (原创)BFS广度优先算法,看完这篇就够了
BFS算法 上一篇文章讲解了DFS深度优先遍历的算法,我们说 DFS 顾名思义DEEPTH FIRET,以深度为第一标准来查找,以不撞南墙不回头的态度来发掘每一个点,这个算法思想get到了其实蛮简单. ...
- UVA10410 TreeReconstruction 树重建 (dfs,bfs序的一些性质,以及用栈处理递归 )
题意,给你一颗树的bfs序和dfs序,结点编号小的优先历遍,问你可能的一种树形: 输出每个结点的子结点. 注意到以下事实: (1)dfs序中一个结点的子树结点一定是连续的. (2)bfs,dfs序中的 ...
- 搜索分析(DFS、BFS、递归、记忆化搜索)
搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...
随机推荐
- C#知识点提炼期末复习专用
根据内部消息称:有三类题型: 程序阅读题:2题 简答题:2题 (主要是对概念的考查) 编程题:暂定2-3题 复习要点: .net framework 通用语言开发环境..NET基础类库..NET ...
- js判断是否手机自动跳转移动端
写法一: {literal} <script> //判断是否手机自动跳转 var browser={versions:function(){var u=navigator.userAgen ...
- skynet inject address file.lua
inject d test/inject_fuck.lua -- d 是服务的 handle 拿 simpledb.lua 举例,修改如下 local skynet = require "s ...
- CentOS 6(64-bit) + Nginx搭建静态文件服务器
Nginx搭建静态文件服务器 使用命令打开Nginx配置文件: sudo vim /etc/nginx/conf.d/default.conf 将配置改为: server { ...... ..... ...
- pcm原始数据绘制
最近帮别人做了个东西,这里分享一下pcm原始数据绘图的思路 1.pcm数据采样位数,根据采样位数选取适合自己绘图的采样点的数量 2.计算出最大最小的的采样点的值差 3.根据要显示pcm数据的控件宽高, ...
- Struts框架核心工作流程与原理
1.Struts2架构图 这是Struts2官方站点提供的Struts 2 的整体结构. 执行流程图 2.Struts2部分类介绍 这部分从Struts2参考文档中翻译就可以了. ActionM ...
- java+hibernate+mysql
实体类News package org.mythsky.hibernatedemo; import javax.persistence.*; @Entity @Table(name="new ...
- 微信小程序(wx:for)遍历对象
最近在折腾微信小程序,遇到这么一个情况:后端返回一个key-value的对象数据,需要遍历对象的key-value,然后渲染到视图中.就像下面这样: { '2018-1-9':{ address: ' ...
- 研究CondItem
- 高可用的MongoDB集群-实战篇
1.概述 最近有同学和网友私信我,问我MongoDB方面的问题:这里我整理一篇博客来赘述下MongoDB供大家学习参考,博客的目录内容如下: 基本操作 CRUD MapReduce 本篇文章是基于Mo ...