Nightmare

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

——————————————————————————————


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>;
#include<queue>
using namespace std;
int mp[10][10];
int vir[10][10];
int m,n,bt,escape;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; struct node
{
int x;
int y;
int bt;
int cnt;
}; bool cheak(int i,int j)
{
if(i<0||i>=m||j<0||j>=n||mp[i][j]==0)
return 0;
return 1;
} int bfs(int si,int sj,int di,int dj)
{
queue<node>q;
node f,d;
f.x=si;
f.y=sj;
f.cnt=0;
f.bt=6;
vir[f.x][f.y]=6;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(f.bt==0)
continue;
if(f.x==di&&f.y==dj)
return f.cnt;
for(int i=0;i<4;i++)
{
d.x=f.x+dir[i][0];
d.y=f.y+dir[i][1];
d.bt=f.bt-1;
if(mp[d.x][d.y]==4&&d.bt!=0)
d.bt=6;
if(cheak(d.x,d.y)&&vir[d.x][d.y]<d.bt)
{
vir[d.x][d.y]=d.bt;
d.cnt=f.cnt+1;
q.push(d);
}
}
}
return -1;
} int main()
{
int o,si,sj,di,dj;
while(~scanf("%d",&o))
{
while(o--)
{
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==2)
{
si=i;
sj=j;
}
if(mp[i][j]==3)
{
di=i;
dj=j;
}
}
}
memset(vir,0,sizeof(vir));
escape=bfs(si,sj,di,dj);
printf("%d\n",escape); } }
return 0;
}

HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏的更多相关文章

  1. POJ2270&&Hdu1808 Halloween treats 2017-06-29 14:29 40人阅读 评论(0) 收藏

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8565   Accepted: 3111 ...

  2. ZUFE2483 DO IT YOURSELF 2017-05-31 14:41 40人阅读 评论(0) 收藏

    2483: DO IT YOURSELF 时间限制: 2 Sec  内存限制: 128 MB 提交: 8  解决: 3 [提交][状态][讨论版] 题目描述 有四个字符串S,T,tmp,ans,一开始 ...

  3. Hdu1978 How many ways 2017-01-18 14:32 40人阅读 评论(0) 收藏

    How many ways Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  4. Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏

    1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...

  5. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

  6. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  7. Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏

    以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...

  8. Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏

    哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  9. Lucene学习总结之四:Lucene索引过程分析 2014-06-25 14:18 884人阅读 评论(0) 收藏

    对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...

随机推荐

  1. 用js来实现银行家算法

    Number.prototype.round = function (len) { var old = this; var a1 = Math.pow(10, len) * old; a1 = Mat ...

  2. oracle表属性

    1. PCTFREE 要形容一个 BLOCK 的运作,我们可以把一个 BLOCK 想成一个水杯.侍者把水倒入放在我们面前的水杯,要多满呢,我们要求他倒 9 分满好了,这时候 PCTFREE 代表着设定 ...

  3. Linux终极渗透测试命令总结

    本文主要和大家分享Linux终极渗透测试命令总结,如下是一份 Linux 机器的渗透测试备忘录,是在后期开发期间或者执行命令注入等操作时的一些典型命令,设计为测试人员进行本地枚举检查之用. 命令 描述 ...

  4. padding-top和margin-top的区别

    学习前端知识,对我们查找页面元素很有帮助,而且自己在原公司时,有参与一个QA系统,自己去设计了这个产品,出了原型图,同时设计了几个页面,希望通过做这个产品提高自己的技术,可是因为离职,所以计划搁浅了, ...

  5. 使用Fiddler发送POST请求

    使用Fiddler发送POST请求 在测试过程中,有时会遇到需要修改请求中带的参数,去验证权限的安全问题,但是一些post请求,我们在浏览器中不能直接修改他的参数,然后去提交验证. 而fiddler可 ...

  6. 安装tftp服务器进行文件传输

    1. 安装: sudo apt-get install tftp-hpa tftpd-hpa ps: tftpd是服务器,tftp是客户端,客户端能发送和获取,服务器不能动. 2. 配置文件: sud ...

  7. Dreamweaver 调字体大小

    编辑--首选参数--字体 这里是指调代码字体的大小,就是为了编码时看的清楚些

  8. Docker commit 制作weblogic镜像

    第一:前提条件 1.本机必须已经安装了docker 容器 2.pull 一个基础的镜像  如图:rastasheep/ubuntu-sshd 第二:利用docker commit  命令 将容器的状态 ...

  9. .net 4.0的Lazy<T>方法,反射实现延迟加载。

    //自己山寨.public class YaLazy<T> { private bool _isValueCreated = false; public bool IsValueCreat ...

  10. Golang之单元测试

    文件名必须以_test.go结尾 使用go test 执行单元测试 例 package main func add(a, b int) int { return a + b } func sub(a, ...