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. 题解 P3205 【[HNOI2010]合唱队】

    讲讲我的做法 看了题目发现要用区间\(dp\),为什么? 我们发现区间\(dp\)有一个性质--大区间包涵小区间,这道题就符合这样的一个性质 所以我们要用区间\(dp\)来解决这道题. 如何设计状态 ...

  2. 动态规划-Minimum Distance to Type a Word Using Two Fingers

    2020-01-12 18:28:13 问题描述: 问题求解: 本题还是非常困难的,至少我在看到这个题目的时候是没有想到怎么解决的.我当时联想到的题目是那条grid走两遍的题目,那条题目也很麻烦,使用 ...

  3. OpenLDAP 多主复制(基于docker容器模式部署)

    **本文主要讲述在docker环境下如何进行 OpenLDAP 多主复制,至于 OpenLDAP 原理可以先参考这篇文章了解:https://cloud.tencent.com/developer/a ...

  4. windows10环境下QtCreator中出现skipping incompatible xxx when searching for xxx 问题解决办法

    windows10环境下QtCreator中出现skipping incompatible xxx when searching for xxx 我再QtCreator中想导入一个外部库时,他提示不匹 ...

  5. ArrayList的传值问题

    ArrayList是一个对象类型,记录一下遇到的传值问题 假设两个ArrayList类型的值a和b,a有值,b无值,想把a的值全部复制给b. 如果使用 b = a; 进行赋值,会将a的地址赋值给b,当 ...

  6. Visdom 介绍 | 一

    用于创建,组织和共享实时丰富数据可视化的灵活工具.支持Python. 概述 概念 设置 用法 API 待办事项 贡献 概述 Visdom旨在促进(远程)数据的可视化,重点是支持科学实验. 为你自己和你 ...

  7. 【深度学习】Neural networks(神经网络)(一)

    神经网络的图解 感知机,是人工设置权重.让它的输出值符合预期. 而神经网络的一个重要性质是它可以自动地从数据中学习到合适的权重参数. 如果用图来表示神经网络,最左边的一列称为输入层,最右边的一列称为输 ...

  8. Ruby学习计划-(1)搭建开发环境

    环境搭建        工欲善其事,必先利其器.要学习一门新的语言当然也需要搭建好开发环境,这样才能更加高效的完成工作提高自身的工作效率.PS:由于自己使用的是MacBookPro,因此之后的所有问题 ...

  9. 8.MSFvenom

    Meterpreter 01 Meterpreter API调用 Meterpreter提供了多种APl调用,在编写自己的脚本时可以使用这些API来提供额外功能或定制功能. 关于ruby的更多信息,请 ...

  10. CodeForces - 817B(分类讨论 + 排列组合)

    题目链接 思路如下 这一题是:最菜的队伍只有三个人组成,我们只需对排序后的数组的 前三个元素进行分类讨论即可: a[3] != a[2] && a[3] != ar[1] a[3] = ...