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

Total Submission(s): 11404    Accepted Submission(s): 5579


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


解题心得:
1、还是可返回路劲,具体的可返回路径看点击打开链接。题不是很难,只要弄清楚初始化的顺序,充分理解好bfs就可以做出来了;
2、这道题中需要求的使用的了最少时间和判断是否可返回的路径没有关系,bfs是按照顺序来的,使用的时间一定是逐步递增的,所以需要判断的是达到每一点还剩余的额的时间。
3、注意初始化位置,不然很尴尬会产生来回走的死循环或者其他的一系列奇怪的问题。


#include<bits/stdc++.h>
using namespace std;
int use[10][10],maps[10][10];
int n,m;
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
struct node
{
int x,y;
int r_time;
int use_time;
}now,Next; bool check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || maps[x][y] == 0)
return false;
else
return true;
} void map_store()
{
memset(use,0,sizeof(use));//注意初始化的位置;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&maps[i][j]);
if(maps[i][j] == 2)//找到起始点
{
use[i][j] = 6;
now.x = i;
now.y = j;
now.r_time = 6;
now.use_time = 0;
}
}
}
} int bfs()
{
queue<node> qu;//每次调用bfs()时会定义一个新的队列;
qu.push(now);
while(!qu.empty())
{
now = qu.front();
qu.pop();//不要忘记了;
for(int i=0;i<4;i++)
{
Next.x = now.x + dir[i][0];
Next.y = now.y + dir[i][1];
Next.use_time = now.use_time + 1;
Next.r_time = now.r_time - 1;
if(Next.r_time == 0)
{
break;
}
if(check(Next.x,Next.y))
{
if(maps[Next.x][Next.y] == 3)
return Next.use_time;
else if(maps[Next.x][Next.y] == 4)
Next.r_time = 6;
if(Next.r_time > use[Next.x][Next.y])
{
qu.push(Next);
use[Next.x][Next.y] = Next.r_time;//use存储时间而不是简单的0-1标记;
}
}
}
}
return -1;//剩余时间用完但是什么都没有找到返回-1;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
map_store();
int Use_time = bfs();
printf("%d\n",Use_time);
}
}

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

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

  5. HDU 1072 Nightmare 题解

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

  6. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

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

  7. [hdu P3085] Nightmare Ⅱ

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

  8. HDU - 3085 Nightmare Ⅱ

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

  9. 题解:HDU 6598

    题解:HDU 6598 Description Now, Bob is playing an interesting game in which he is a general of a harmon ...

  10. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

随机推荐

  1. POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】

    Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  2. Xpath定位绝密版本

    xpath的作用就是两个字“定位”, 运用各种方法进行快速准确的定位,推荐两个非常有用的的firefox工具:firebug和xpath checker 在 XPath 中, 有七种类型的节点:元素. ...

  3. DataBinding初探 数据绑定的用法 ,import 集合类型,绑定的表达式,访问集合类型2

    数据绑定的用法 import语法   <data> <import type="android.view.view"/> </data>   如 ...

  4. 让我们把KBEngine玩坏吧!如何定制我们自己的C++函数(一)

    为什么不更新kbe warring的代码解读了,因为在我看来那个demo讲完了实体就没东西可讲了,如果专心的看官方文档和PPT的话demo的代码后面没任何难点了已经,单纯的复制黏贴代码实在太过无聊.程 ...

  5. 生产消费者模式与python+redis实例运用(基础篇)

    根据这个图,我们举个简单的例子:假如你去某个餐厅吃饭,点了很多菜,厨师要一个一个菜的做,一个厨师不可能同时做出所有你点的菜,于是你有两个选择:第一个,厨师把所有菜都上齐了,你才开始吃:还有一个选择,做 ...

  6. 如何进行大规模在线数据迁移(来自Stripe公司的经验)

    首发于笔者的微信公众号:技术心流FollowFlows 原文地址:Stripe Engineering Blog   各工程团队常面临一项共同挑战:重新设计数据模型以支持清晰准确的抽象和更复杂的功能. ...

  7. iOS多线程系统整理 swift

    多线程   是一个应用程序内多个代码的执行路径,执行线程,同时在同一时间里执行不同的任务. 三种: 1.NSTread 2.Cocoa NSOperation (NSOperation,NSOpera ...

  8. axios使用配置

    axios 配置 下载cnpm install axios vue-axios --save-dev main.js文件中配置 import axios from 'axios' import Vue ...

  9. Eucalyptus简介

    1.Eucalyptus是什么? Eucalyptus  n.桉树 Eucalyptus is a Linux-based software architecture that implements ...

  10. C++编写字符串类CNString,该类有默认构造函数、类的拷贝函数、类的析构函数及运算符重载

    编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下“=”运算符.“+”运算.“[]”运算符.“<”运算符及“>”运算符及“==”运算符 ...