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 ...
随机推荐
- 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter
[源码下载] 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresen ...
- sax解析xml,验证格式并支持自定义标签
一.sax简介 SAX是事件驱动型的XML解析方式.顺序读取XML文件,生成事件,传播到用户定义的回调方法中来处理XML文件. 优点: 分段处理xml,而不是将整个xml一次加载进内存,内存占用少,速 ...
- Swift5 语言参考(四) 表达式
在Swift中,有四种表达式:前缀表达式,二进制表达式,主表达式和后缀表达式.评估表达式会返回一个值,导致副作用,或两者兼而有之. 前缀和二进制表达式允许您将运算符应用于较小的表达式.主要表达式在概念 ...
- JavaScript Boolean( new Boolean(false) ) 其实是true
Boolean类型是JavaScript原始数据类型(primitive type)之一:常用来表示 真或假,是或否:这个类型只有两个值:保留字true和false 一般用于控制语句:如下 if(Bo ...
- redo log文件格式描述
- asp.net core中遇到需要自定义数据包解密方法的时候
最近将公司的项目用.netcore重写, 服务的http外部接口部分收发消息是DES加解密的, 那么在asp.net core mvc的action处理之前需要加入解密这个步骤. 我第一想到的是用fi ...
- PHP设计模式:观察者模式
示例代码详见https://github.com/52fhy/design_patterns 观察者模式 观察者模式(Observer)是对象的行为模式,又叫发布-订阅(Publish/Subscri ...
- OSRM笔记
OSRM OSRM(OpenStreetMap Routeing Machine)可用于路线规划.作为高性能的路线规划引擎,OSRM使用C++14编写,基于开源的OpenStreetMap数据实现. ...
- Searching with Deep Learning 深度学习的搜索应用
本文首发于 vivo 互联网技术微信公众号 https://mp.weixin.qq.com/s/wLMvJPXXaND9xq-XMwY2Mg作者:Eike Dehling翻译:杨振涛 本文由来自 T ...
- java监听器、定时器的使用
1.监听器 在web.xml配置 <!-- 时间任务 --> <listener> <listener-class> com.hk.common.timer.Tim ...