Nightmare BFS
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的更多相关文章
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- HDUOJ-----(1072)Nightmare(bfs)
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- hdoj1072 Nightmare bfs
题意:在一个地图里逃亡,2是起点,3是终点,1是路,0是墙,逃亡者携带一个炸弹,6分钟就会炸,要在6分钟前到达4可以重制时间,问是否能逃亡,若能则输出最小值 我的思路:bfs在5步内是否存在3,存在则 ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- nyoj 483 Nightmare【bfs+优先队列】
Nightmare 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found him ...
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- Nightmare Ⅱ HDU - 3085 (双向bfs)
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...
- hdu1072(Nightmare)bfs
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- 201771010103 陈亚茹 《面向对象程序设计(java)》第一周学习总结
本人学号<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com https://w ...
- win10系统 端口查看问题。
首先看图根据系统自带命令netsta介绍,说明显示协议系统信息和当前TCP/IP 网络连接. 使用范例: 打开命令提示符窗口,在这里输入命令netstat -an,然后按下回车键,这时可以显示出电脑中 ...
- Flutter 实现整个App变为灰色
在Flutter中实现整个App变为灰色是非常简单的,只需要在最外层的控件上包裹ColorFiltered,用法如下: @override Widget build(BuildContext cont ...
- coding++:error Could not read JSON: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object
Spring源码中是使用容器中的ObjectMapper对象进行序列化和反序列化. 当我们将自定义的ObjectMapper对象放入IOC容器中后,会自动覆盖SpringBoot自动装载的Object ...
- [codevs2597]团伙<并查集>
题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友 ...
- MATLAB GUI设计(3)
一.gca.gcf.gco 1.三者的功能定义: gcf 返回当前Figure 对象的句柄值 gca 返回当前axes 对象的句柄值 gco 返回当前鼠标单击的句柄值,该对象可以是除root 对象外的 ...
- 用c#判断回文数和降序数
题目:编一个程序,输入一个正整数,判定它是否为回文数和降序数.当输入的数为0时,则退出程序,否则继续循环执行程序. 所谓“降序数”是指一个自然数的低位数字不大于高位数字的数.例如: 64, 55, 3 ...
- A 大地魂力
时间限制 : - MS 空间限制 : - KB 评测说明 : 1s,256m 问题描述 奶牛贝西认为,要改变世界,就必须吸收大地的力量,贝西把大地的力量称为魂力.要吸取大地的魂力就需要在地上开出 ...
- Reface.NPI 方法名称解析规则详解
在上次的文章中简单介绍了 Reface.NPI 中的功能. 本期,将对这方法名称解析规则进行详细的解释和说明, 以便开发者可以完整的使用 Reface.NPI 中的各种功能. 基本规则 方法名称以 I ...
- 《SEMI-SUPERVISED CLASSIFICATION WITH GRAPH CONVOLUTIONAL NETWORKS》论文阅读
背景简介 GCN的提出是为了处理非结构化数据(相对于image像素点而言).CNN处理规则矩形的网格像素点已经十分成熟,其最大的特点就是利用卷积进行①参数共享②局部连接,如下图: 那么类比到非结构数据 ...