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
 

题目有点长呀~~读懂题目其实就好做了。

题目大意就是:在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间。但是要注意的是:地图可以重复访问,但是实际上,如果是对于地图上是4的点,重复去访问的话,没有意义,因为如果走的出去,走一遍是4的就可以了,重复去走只会增加步数,而如果走不出去。。。更没必要了,所以,可以对4的点进行标记。

//Asimple
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
using namespace std;
const int maxn = ;
int dx[] = {-,,,}, dy[]={,,-,};
typedef long long ll;
int n, m, num, T, k, x, y, len, ans;
int endx, endy;
int Map[maxn][maxn]; struct node{
int x;
int y;
int step;
int time;
};
node start; bool wrong(int x, int y) {
return x< || x>=n || y< || y>=m || !Map[x][y];
} void BFS() {
queue<node> q;
node now, next;
q.push(start);
while( !q.empty() ) {
now = q.front();
q.pop();
for(int i=; i<; i++) {
next.step = now.step+;
next.time = now.time-;
next.x = now.x + dx[i];
next.y = now.y + dy[i];
if( !wrong(next.x, next.y) && next.time> ) {
if( Map[next.x][next.y] == ) {
cout << next.step << endl;
return ;
} else if( Map[next.x][next.y] == ) {
next.time = ;
Map[next.x][next.y] = ;
}
q.push(next);
}
}
}
cout << - << endl;
} void input() {
cin >> T ;
while( T -- ) {
cin >> n >> m;
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
cin >> Map[i][j];
if( Map[i][j] == ) {
start.x = i;
start.y = j;
}
}
}
start.step = ;
start.time = ;
BFS();
}
} int main(){
input();
return ;
}

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 (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  4. HDU 1072 Nightmare 题解

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

  5. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. [hdu P3085] Nightmare Ⅱ

    [hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  7. HDU - 3085 Nightmare Ⅱ

    HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...

  8. HDU 1072 (不一样的入队条件) Nightmare

    之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...

  9. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

随机推荐

  1. Online, Asynchronous Schema Change in F1

    F1: A Distributed SQL Database That Scales   http://disksing.com/understanding-f1-schema-change   ma ...

  2. jQuery 实时监听<input>输入值的变化

    这方法比 on('keydown') 更实时 <input type='text' id='input1'/>$(document).ready(function(){ $('#input ...

  3. 一些NSArray,NSDictionary,NSSet相关的算法知识

    iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...

  4. SublimeText教程

    1安装插件 1.請先確認已經安裝好Package Control 未安裝的話請看:安裝第一個Sublime Text套件 2.按下CTRL + SHIFT + P並且輸入Package Control ...

  5. javascript的缓动效果

    这部分对原先的缓动函数进行抽象化,并结合缓动公式进行强化.成品的效果非常惊人逆天.走过路过不要错过. 好了,打诨到此为止.普通的加速减速是难以让人满意的,为了实现弹簧等让人眼花缭乱的效果必须动用缓动公 ...

  6. javascript客户端与服务器端通信

    高性能的网络通信包括以下方面:选择正确的数据格式和与之匹配的传输技术. 一.数据格式 用于传输的数据格式有: 1)html,仅适用于特定场合,传输数据量大,不过它可以节省客户端的CPU周期, 2)XM ...

  7. word search puzzle

    package WordSearch; import java.util.ArrayList; import java.util.HashMap; import java.io.*; public c ...

  8. lua 面向对象编程类机制实现

    lua no class It is a prototype based language. 在此语言中没有class关键字来创建类. 现代ES6, 已经添加class类. prototype bas ...

  9. .NET 扩展方法(Extention Method)的要点

    扩展方法Extention Method的主要介绍在:http://msdn.microsoft.com/zh-cn/library/bb383977(v=vs.100).aspx. 扩展方法的意义在 ...

  10. querystring 解析url 查询字符串

    对前端同学来说,经常要碰到一种比较麻烦的情况,那就是url查询字符串的解析问题.说起来也不难,就是比较麻烦. 具体来处理这种情况的时候,相信有一部分同学就是针对具体项目中的需要的字符去正则匹配一下,业 ...