Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5647    Accepted Submission(s): 2808

Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.
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.
 
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth. There are five integers which indicate the different type of area in the labyrinth: 0: The area is a wall, Ignatius should not walk on it. 1: The area contains nothing, Ignatius can walk on it. 2: Ignatius' start position, Ignatius starts his escape from this position. 3: The exit of the labyrinth, Ignatius' target position. 4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
 
Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
 
Sample Input
3
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
 
Sample Output
4
-1
13
 
 

这题不同于其他题的地方就是于虽然也是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的更多相关文章

  1. 迷宫寻宝(一)(bfs)

    迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编 ...

  2. python 网络爬虫(二) BFS不断抓URL并放到文件中

    上一篇的python 网络爬虫(一) 简单demo 还不能叫爬虫,只能说基础吧,因为它没有自动化抓链接的功能. 本篇追加如下功能: [1]广度优先搜索不断抓URL,直到队列为空 [2]把所有的URL写 ...

  3. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

  4. HDOJ(1242)BFS+优先队列

    Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:"#"是墙,"."是路,"a&quo ...

  5. nyist 82迷宫寻宝(一)(BFS)

    题目连接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 此题在基础BFS上加入了门和钥匙,要找齐所有钥匙才能开门,所以要对门特殊处理. 1.先 ...

  6. LeetCode 5073. 进击的骑士(Java)BFS

    题目:5073. 进击的骑士 一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里. 骑士的走法和中国象棋中的马相似, ...

  7. (原创)BFS广度优先算法,看完这篇就够了

    BFS算法 上一篇文章讲解了DFS深度优先遍历的算法,我们说 DFS 顾名思义DEEPTH FIRET,以深度为第一标准来查找,以不撞南墙不回头的态度来发掘每一个点,这个算法思想get到了其实蛮简单. ...

  8. UVA10410 TreeReconstruction 树重建 (dfs,bfs序的一些性质,以及用栈处理递归 )

    题意,给你一颗树的bfs序和dfs序,结点编号小的优先历遍,问你可能的一种树形: 输出每个结点的子结点. 注意到以下事实: (1)dfs序中一个结点的子树结点一定是连续的. (2)bfs,dfs序中的 ...

  9. 搜索分析(DFS、BFS、递归、记忆化搜索)

    搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...

随机推荐

  1. Entity Framework 6 多对多增改操作指南

    问题描述 在很多系统中,存在多对多关系的维护.如下图: 这种多对多结构在数据库中大部分有三个数据表,其中两个主表,还有一个关联表,关联表至少两个字段,即左表主键.右表主键. 如上图,其中的Suppli ...

  2. Fiddler工具使用介绍二

    在上一篇中介绍了Fiddler的基本使用方法.通过上一篇的操作我们可以直接抓取浏览器的数据包.但在APP测试中,我们需要抓取手机APP上的数据包,应该怎么操作呢? Andriod配置方法: 1)确保手 ...

  3. urllib2 的使用与介绍

    爬虫简介  什么是爬虫? 爬虫:就是抓取网页数据的程序. HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的 ...

  4. [Ynoi2018]未来日记(分块)

    分块神题. 看了一会儿题解,看懂了思路,然后写了两个小时,调了一个多小时,好多地方写错了. 我们考虑对序列和值域都分块.\(sum1[i][j]\) 表示前 \(i\) 个块,第 \(j\) 块值域有 ...

  5. MYSQL基础语法的使用

    喜欢的朋友可以关注下,粉丝也缺. MYSQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之 ...

  6. 【xsy2913】 enos 动态dp

    题目大意:给你一棵 $n$个点 以 $1$为根 的树,每个点有$ 0,1,2 $三种颜色之一,初始时整棵树的颜色均为 $0$. $m$ 次操作, 每次操作形如: 1 x y c : 将 $x$到$y$ ...

  7. vue教程2-03 vue计算属性的使用 computed

    vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...

  8. 前端安全 -- XSS攻击

    XSS漏洞是最广泛.作用最关键的web安全漏洞之一.在绝大多数网络攻击中都是把XSS作为漏洞链中的第一环,通过XSS,黑客可以得到的最直接利益就是拿到用户浏览器的cookie,从而变相盗取用户的账号密 ...

  9. MVC3学习:利用mvc3+ajax实现级联下拉列表框

    本例使用的是EF first code操作数据库. 一.准备数据库 级联下拉列表框,比较经典的就是省市数据表,在Model里同时创建三个类:province.cs.city.cs和dropContex ...

  10. Storm的acker确认机制

    Storm的acker消息确认机制... ack/fail消息确认机制(确保一个tuple被完全处理) 在spout中发射tuple的时候需要同时发送messageid,这样才相当于开启了消息确认机制 ...