题目链接

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

分析:

刚开始拿到这道题的时候,以为就是和原先一样的普通的广搜,标记走过的路不能够再走,但是发现这样行不通,后来才发现并不是所有走过的路都不能走了,只是有炸弹重装装置的那个店不能够再走了,只需要标记这个点已经走过就行了。

0:表示墙,不能够走

1:空地,可以从这走

2:起始点

3:终点

4:炸弹重装装置

题目要求算出从其实带你到终点所花费的时间,他每走一个点就会花费一分钟。但是他的身上还有个距离爆炸时间为6分钟的炸弹,必须的保证他在走到每个点的时候炸弹没有爆炸,而且当他走到炸弹重装装置的时候,炸弹的爆炸时间就会又变为6分钟。

代码:

    #include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int map[8][8];
int n,m;
struct node
{
int x,y,step,time; //step为走的步数,time为bomb离爆炸时间
}start;
void store_map()//将地图的形式表示出来,并且找到起始点
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
{
start.x=i;
start.y=j;
start.step=0;
start.time=6;
}
}
}
}
void bfs()
{
const int help[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//能走的四个方向
queue<node> q;
q.push(start);
node p1,p2;
int i;
while(!q.empty())
{
p1=q.front();
q.pop();
for(i=0;i<4;i++)
{
p2.step=p1.step+1;//步数加
p2.time=p1.time-1;//时间减
p2.x=p1.x+help[i][0];
p2.y=p1.y+help[i][1];
if(p2.x>=0 && p2.x<n && p2.y>=0 && p2.y<m && map[p2.x][p2.y]!=0 && p2.time>0)
//注意这里的p2.time>0,意味着炸弹没有爆炸
{
if(map[p2.x][p2.y]==3)//重点的话就输出
{
printf("%d\n",p2.step);
return;
}
else if(map[p2.x][p2.y]==4)//炸弹重装装置就把时间变为6
{
p2.time=6;
map[p2.x][p2.y]=0; //并且该位置4不能再访问了
}
q.push(p2);
}
}
}
printf("-1\n");//队空的话,也就意味着没有找到终点
}
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&n,&m);
store_map();
bfs();
}
return 0;
}

HDU 1072 Nightmare (广搜)的更多相关文章

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

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

  2. hdu - 1072 Nightmare(bfs)

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

  3. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  4. HDU 1072 Nightmare 题解

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

  5. hdu 1495 非常可乐 广搜

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> ][][]; ...

  6. hdu 1342.. 复习广搜 顺便练习一下一个脑残的格式

    In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49}. A popular stra ...

  7. hdu 1072 广搜(逃离爆炸迷宫)

    题意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间 ...

  8. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

随机推荐

  1. java 基础 --File

    1, 创建文件 File file = new File(path); file.createNewFile(); //如果路径不存在,会抛异常 file.mkdir();//如果路径不存在,返回fa ...

  2. ZOJ3113_John

    这个题目是一个典型的Anti_Sg.我也不知道为什么这么叫,呵呵,反正大家都这么叫,而且我也是听别人说,看别人的日志自己才知道的. 题目的意思是给你不同颜色的石子,每次可以去一种颜色的石子若干个(至少 ...

  3. 【刷题】BZOJ 4650 [Noi2016]优秀的拆分

    Description 如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串,则我们称该字符串的这种拆分是优秀的.例如,对于字符串 aabaabaa,如果令 A ...

  4. 【刷题】BZOJ 1036 [ZJOI2008]树的统计Count

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u t : 把结点u的权值改为t II. ...

  5. 【MediaElement】WPF视频播放器【1】

    一.前言       前两天上峰要求做一个软件使用向导,使用WPF制作.这不,这两天从一张白纸开始学起,做一个播放演示视频的使用向导.以下是粗设计的原型代码: 二.效果图 三.代码 前台代码: < ...

  6. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  7. 扔几道sb题

    1.给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍区间. 你能求 ...

  8. Hbase(一)基础知识

    一.Hbase数据库介绍 1.简介 HBase 是 BigTable 的开源 java 版本.是建立在 HDFS 之上,提供高可靠性.高性能.列存储. 可伸缩.实时读写 NoSQL 的数据库系统. N ...

  9. Codeforces Round #441 Div. 2题解

    比赛的时候E调了好久...F没时间写T T A:直接走到短的路上来回走就好了 #include<iostream> #include<cstring> #include< ...

  10. 【bzoj4872】【shoi2017】分手即是祝愿

    4872: [Shoi2017]分手是祝愿 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 746  Solved: 513[Submit][Statu ...