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. 

InputThe 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. 
OutputFor 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
题目的大意就是 迷宫4处可以重置炸弹,,0是墙壁,1是空地,2是起点,3是终点,每个地方都可重复走,判断能否到达终点
这里有坑就是每个地方可以重复的走,位置4可以多次重置炸弹(这就是个坑)。。。因为当第二次来同一个位置4重置炸弹时,,OK,,一定构成了循环,,一定走不出去了。所以想要走出去同一个位置4 只能去一次,,所以要标记一下。然后BFS就可以了
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct stu{
int x,y,z;
}; int step[][];
int d[][]={{,,-},{,,-},{,-,-},{-,,-}};
int n,m;
int flag=;
int start_x,start_y,end_x,end_y;
int mark[][];
int arr[][]; void bfs(){
queue<stu>que;
que.push({start_x,start_y,});
step[start_x][start_y]=;
while(que.size()){
int xx=que.front().x;
int yy=que.front().y;
int zz=que.front().z; que.pop();
for(int i=;i<;i++){ int dx=xx+d[i][];
int dy=yy+d[i][];
int dz=zz+d[i][]; if(dz>&&dx>=&&dy>=&&dx<n&&dy<m&&arr[dx][dy]!=&&mark[dx][dy]==)
{
step[dx][dy]=step[xx][yy]+;
if(arr[dx][dy]==){
flag=;
return ;
} if(arr[dx][dy]==){
dz=;
mark[dx][dy]=;
}
que.push({dx,dy,dz});
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(mark,,sizeof(mark));
flag=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%d",&arr[i][j]);
}
}
for(int i=;i<n;i++)
for(int j=;j<m;j++){
if(arr[i][j]==){
start_x=i;
start_y=j;
}
else if(arr[i][j]==){
end_x=i;
end_y=j;
}
}
bfs();
if(flag)
printf("%d\n",step[end_x][end_y]);
else puts("-1");
}
return ;
}
												

Nightmare BFS的更多相关文章

  1. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  2. HDUOJ-----(1072)Nightmare(bfs)

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. hdoj1072 Nightmare bfs

    题意:在一个地图里逃亡,2是起点,3是终点,1是路,0是墙,逃亡者携带一个炸弹,6分钟就会炸,要在6分钟前到达4可以重制时间,问是否能逃亡,若能则输出最小值 我的思路:bfs在5步内是否存在3,存在则 ...

  4. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  5. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. nyoj 483 Nightmare【bfs+优先队列】

    Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Ignatius had a nightmare last night. He found him ...

  7. HDU-1072 Nightmare (bfs+贪心)

    Nightmare Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  8. Nightmare Ⅱ HDU - 3085 (双向bfs)

    Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...

  9. hdu1072(Nightmare)bfs

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. linux-manjaro下添加Yahei Hybrid Consola字体

    1.下载地址 http://www.win10zhijia.net/soft/20160921/3217.html 2.解压 unzip xxx 3.安装 sudo mkdir /usr/share/ ...

  2. JSP+Servlet+C3P0+Mysql实现的azhuo商城

    项目简介 项目来源于:https://gitee.com/xuyizhuo/shopping 原仓库中缺失jar包及sql文件异常,现将修改过的源码上传到百度网盘上. 链接:https://pan.b ...

  3. Vmware15.5安装与许可教程

    最近Windows总是提醒我1803版本的服务即将过期,劝我升级到最新版.可我在自动安装的过程中却总是安装失败.于是官网下载了更新助手.检测到的问题是升级过程和 Vmware 软件冲突,于是卸载了 V ...

  4. 【MySQL】面试官:谈谈你对Mysql的MVCC的理解?

    MVCC(Mutil-Version Concurrency Control),就是多版本并发控制.MVCC 是一种并发控制的方法,一般在数据库管理系统中,实现对数据库的并发访问. 在Mysql的In ...

  5. 开发一个健壮的npm包

    项目地址:loan-calculate-utils npm包的发布.更新查看上一篇文章 开发一个基础的npm包 目前我们的目录是这个样子: . ├── source 源代码目录 │   └── ind ...

  6. 倒计时器CountDownLatch

    1.背景: countDownLatch是在java1.5被引入,跟它一起被引入的工具类还有CyclicBarrier.Semaphore.concurrentHashMap和BlockingQueu ...

  7. 在MVC三层项目中如何使用Log4Net

    --前期准备(添加到队列中) 0-1在新建后的MVC项目中的[Models]中添加一个类,用于处理异常信息,并继承自HandleErrorAttribute public class MyExcept ...

  8. 一个基于深度学习回环检测模块的简单双目 SLAM 系统

    转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12634631.html 写在前面 最近在搞本科毕设,关于基于深度学 ...

  9. Spring Cache 缺陷,我好像有解决方案了

    Spring Cache 缺陷 Spring Cache 是一个非常优秀的缓存组件. 但是在使用 Spring Cache 的过程当中,小黑同学也遇到了一些痛点. 比如,现在有一个需求:通过多个 us ...

  10. WPF使用 Gmap.NET 绘制极坐标运动轨迹

    大家好,已经很久没有更新了,今天写一篇关于WPF 使用 Gmap.NET 相关的,网上很多Winform的很**,所以我给Wpf进行一些补充.虽然它已经很久没有更新了,但是也只能用这个了.没别的好选择 ...