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. 关于ASP.NET MVC4在IIS6中不认识安卓移动设备的解决办法

    在IIS7中发现安卓的手机浏览器是可以跳转滴. 项目中是采用***.mobile.cshtml来显示移动视图的. 部署到IIS6.0中发现并没有转到*.mobile.cshtml移动视图. 进过漫长的 ...

  2. ReactiveCocoa 源码阅读记录。

    1:RACSingle 需要订阅信号 RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACS ...

  3. Spring Boot中使用@Scheduled创建定时任务

    我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时 ...

  4. Docker - 基础讲义

    Docker Docker - 官网 Docker - Hub GitHub - Docker dockerinfo Docker中文社区 Docker入门教程 Docker从入门到实践 虚拟化技术 ...

  5. POJ 2421

    #include<iostream> #include<stdio.h> #define MAXN 500 #define inf 100000000 using namesp ...

  6. (转)percona的安装、启动、停止

    原文:https://blog.csdn.net/tanliqing2010/article/details/78758878 socket=/percona/3307/data/mysql.sock ...

  7. java重点知识

    一.java基本知识点 java是由SUN公司在1995年推出的,在2009年SUN公司又被甲骨文公司收购,所以甲骨文公司获得java版权.其底层是由C语言写的,java分为三个体系: JavaSE, ...

  8. TensorFlow.js之根据数据拟合曲线

    这篇文章中,我们将使用TensorFlow.js来根据数据拟合曲线.即使用多项式产生数据然后再改变其中某些数据(点),然后我们会训练模型来找到用于产生这些数据的多项式的系数.简单的说,就是给一些在二维 ...

  9. Android使用内容提供者实现增删改查操作

    Android使用内容提供者实现增删改查操作 这里需要建立两个项目:SiYouShuJuKu(使用内容提供者暴露相关的操作),DQDYGApplication(使用内容解析者对第一个应用进行相关的解析 ...

  10. Google Optimization Tools实现员工排班计划Scheduling【Python版】

    上一篇介绍了<使用.Net Core与Google Optimization Tools实现员工排班计划Scheduling>,这次将Google官方文档python实现的版本的完整源码献 ...