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. Xamarin Android 绑定jar库同时将so库打包进去

    1.在创建的Bindings Library项目中,新建Assets目录: 2.Assets目录下再分别创建armeabi,armeabi-v7a,x86三个目录: 3.将so文件分布copy到三个目 ...

  2. Android--ListView与数据绑定(Xamarin)

    ListView 控件是一个条目容器, 用于显示集合对象(如数组, List<T>, ObservableCollection<T>等)的每一个条目, 并提供滚动功能. 列表视 ...

  3. 关于 windows 下 node_modules\node-sass\vendor 的报错解决方法

    项目git clone下来之后,运行npminstall, npm start报错代码如下: ERROR in ENOENT: no such file or directory, scandir ' ...

  4. android模拟器没有键盘的解决方法

    刚开始使用android模拟器的时候,发现自己创建的AVD启动后没有出现侧边的键盘,在网上搜索后,发现很多人都有这个问题,也有文章说直接使用PC上的键盘,因为有对应的快捷键.但是,没有键盘,始终不爽! ...

  5. OC ---- 字典集合 iOS学习-----细碎知识点总结

    实例方法的创建 NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:", @"age" ...

  6. 《storm实战-构建大数据实时计算读书笔记》

    自己的思考: 1.接收任务到任务的分发和协调   nimbus.supervisor.zookeeper 2.高容错性                            各个组件都是无状态的,状态 ...

  7. Android pop3与imap方式接收邮件(javamail)

    需要下载3个jar包:mail.jar/    activation.jar/    additionnal.jar 1.pop3 /** * 以pop3方式读取邮件,此方法不能读取邮件是否为已读,已 ...

  8. c++的重载运算符

    c++中允许重载运算符: 这是我辛苦的结果 #include"iostream"using namespace std;class aaa{ int x;public: aaa() ...

  9. Windows Phone 六、JSON序列化

    JSON序列化 public class Person { public int Id { get; set; } public string Name { get; set; } public in ...

  10. 据库都有哪些锁 然后 Kill session

    当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键时或者表的索引时,常常会出现ora-00054:resource busy and acquire with nowait ...