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. canvas.toDataURL()报错的解决方案全都在这了

    报错详尽信息 Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases ...

  2. Dropout的前世与今生

    Dropout 是一类用于神经网络训练或推理的随机化技术,这类技术已经引起了研究者们的广泛兴趣,并且被广泛地应用于神经网络正则化.模型压缩等任务.虽然 Dropout 最初是为密集的神经网络层量身定制 ...

  3. 科普 | ​生成对抗网络(GAN)的发展史

    来源:https://en.wikipedia.org/wiki/Edmond_de_Belamy 五年前,Generative Adversarial Networks(GANs)在深度学习领域掀起 ...

  4. ehcahe + redis 实现多级缓存

    1,了解数据存储的位置的不同 数据库:存储在磁盘上 redis:存储在内存上 ehcache:应用内缓存 缓存的目的:是为了将数据从一个较慢的介质上读取出来,放到一个较快的介质上,为了下次读取的时候更 ...

  5. sql MariaDB 安装contos

    安装和运行MySQL数据库(MariaDB) centos 平台 1.安装和运行 yum install mariadb mariadb-server - 安装 systemctl start mar ...

  6. windows server 2016 远程桌面mstsc DPI(更改文本、应用和其他项目大小) 设置

    windows server 2016 远程桌面mstsc DPI 设置 在高分辨率机器2K,4K,8K,登入使用window远程桌面mstsc时,登入后虽然分辨率变成了和cilent一样分辨率 但是 ...

  7. Python input和print函数

    一.input函数 可以看出,input()函数默认输入的是字符串类型,需要eval()函数将其进行转换. 区别直接赋值的情况,Python可以自动识别数据类型 二.print函数 1.直接输出 无论 ...

  8. 关于k12领域Lucene.net+pangu搜索引擎设计开发的一些回顾

    在中国最大的教育资源门户网站两年期间, 黄药师负责学科网搜吧的设计与开发…正好赶上了公司飞速发展的阶段.. 作为专注于k12领域内容与服务的互联网公司的一员,同时整个公司又在积极提升用户体验的氛围中, ...

  9. 6L-单向链表实现

    关注公众号 MageByte,有你想要的精彩内容.文中涉及的代码可访问 GitHub:https://github.com/UniqueDong/algorithms.git 上一篇<链表导论心 ...

  10. java+lodop+vue+热敏打印机,打印图片

    1.根据需求生成图片模板,详情见 https://www.cnblogs.com/xiaokangk/p/11151774.html 2.下载lodop并进行安装(安装步骤详情百度) 3.安装热敏打印 ...