题目链接

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;
}

最新文章

  1. 代码生成工具——Entity Framework Power Tools
  2. CAD迷你看图
  3. [阅读]个人阅读作业week7(200)
  4. 安卓Design包之CollapsingToolbarLayout(可折叠的工具栏布局)的简单使用
  5. WIN10主动推升级,有点意思
  6. java_jdbc_反射技术将查询结果封装为对象
  7. cocos2d-x中使用JNI的调用JAVA方法
  8. redis 配置(1)
  9. 实验:Oracle数据泵导出导入之序列问题
  10. PyQt5安装目录中找不到designer.exe与pyrcc5.exe
  11. C盘里的桌面文件移到E盘里了,然后E盘里的文件都显示到桌面上了,怎么将桌面文件还原回C盘
  12. [NOI 2016]区间
  13. Android进阶加密-第1章-Android系统架构-读书笔记
  14. gcc:call to &#39;__open_missing_mode&#39; declared with attribute error
  15. CSS 浮动(float)与定位(position)
  16. IPv6地址测试宏
  17. 2019/3/27 wen 数组排序
  18. 《GPU高性能编程CUDA实战》第十章 流
  19. 基于MongoDB2.6版本配置MongoDB主从复制集群架构
  20. 双11怎么那么强!之二:浅析淘宝网络通信库tbnet的实现

热门文章

  1. 【第八周】【新蜂】新NABCD
  2. python接口自动化测试框架实现之字符串插入变量(字符串参数化)
  3. Win2019 + Oracle18c SQLPLUS 命令行出现乱码的解决
  4. webgl学习笔记三-平移旋转缩放
  5. luogu 1967 货车运输(最大生成树+LCA)
  6. 【bzoj1004】[HNOI2008]Cards Burnside引理+背包dp
  7. BZOJ4899 记忆的轮廓(概率期望+动态规划+决策单调性)
  8. TCP的拥塞控制 (二)
  9. 前端开发学习之——利用模板实现涉及url问题时的bug分析及解决(chrome源码)
  10. 【BZOJ1391】Order(网络流,最小割)