#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

class Data
{
public:
int Etime;
int x, y;
int count;
};

int n, m, sx, sy;
int map[9][9];
int direction[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

int bfs() //广度搜索
{
int i;
int tx, ty;
queue<Data> Que;
while(!Que.empty())
{
Que.pop();
}
Data Da, temp;
Da.x = sx; Da.y = sy;
Da.Etime = 6; Da.count = 0;
Que.push(Da); //起点入队
while(!Que.empty())
{
temp = Que.front();
Que.pop();
if(map[temp.x][temp.y] == 3) //到了终点
return temp.count;
if(temp.Etime == 1) //没到终点,时间变成1,下一步之后,时间变0,无论怎么走,都没时间了,直接跳过
continue; //忽略掉时间为0的,下面的引爆就不用判断时间
for(i = 0; i < 4; i++) //四个方向搜索
{
tx = temp.x + direction[i][0];
ty = temp.y + direction[i][1];
if(tx < 0 || tx >= n || ty < 0 || ty >= m || map[tx][ty] == 0)
continue;

Da.x = tx; Da.y = ty; Da.Etime = temp.Etime - 1; Da.count = temp.count + 1;
if(map[tx][ty] == 4) //引爆,重置时间
{
Da.Etime = 6;
map[tx][ty] = 0;
}
Que.push(Da);
}
}
return -1;
}

int main()
{
// freopen("data.txt", "r", stdin);
int T, i, j;
while(scanf("%d", &T) != EOF)
{
while(T--)
{
scanf("%d%d", &n, &m);
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
scanf("%d", &map[i][j]);
if(map[i][j] == 2) //找到起点
{
sx = i;
sy = j;
}
}
int ans = bfs();
cout << ans << endl;
}
}
return 0;
}

hdu1072的更多相关文章

  1. HDU-1072 Nightmare (bfs+贪心)

    Nightmare Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  2. hdu1072 Nightmare---BFS

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重 ...

  3. Nightmare HDU1072

    非常标准的BFS 第一次写错了很多 1.到达4时设置为墙就好了  避免了死循环 2.不用开d数组   在结构体里面就行了 3.结构体初始化函数的写法: Node(int x=0,int y=0,int ...

  4. hdu1072(Nightmare)bfs

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

  5. HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏

    Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...

  6. hdu1072(bfs)

    #include<iostream> #include<queue> #include<cstring> using namespace std; int a[10 ...

  7. HDU1072:Nightmare

    传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...

  8. HDU1072:Nightmare [DFS]

    题目链接:Nightmare 题意: 给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点 问是否能从起点到达终点 分析: 一道很好的DFS题目,炸弹重置点必然最多走 ...

  9. hdu1072 逃离迷宫系列 bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时 ...

随机推荐

  1. wget 实现web监控脚本

    #!/bin/sbin timeout= times= url=https://1.1.1.1 while true;do wget --no-check-certificate --timeout= ...

  2. 使用ksar解析sar监控日志

    sar 是属于sysstat包中的一个工具 安装sysstat包后,默认创建一个/etc/cron.d/sysstat文件,其默认内容为: # run system activity accounti ...

  3. mysql自定义函数语法

    创建函数 DELIMITER $$DROP FUNCTION IF EXISTS `test` $$CREATE FUNCTION `test`(a int ,b int)RETURNS int BE ...

  4. 剑指offer之 栈的压入、弹出序列

    题目描述:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出序列.假设压入栈的所有数字均不相等.例如序列1/2/3/4/5是某栈的压栈序列,序列4/5/3/2/1是该压栈序 ...

  5. castle windsor学习-----Registering components one-by-one 注册类型

    1.在容器中注册一个类型 container.Register( Component.For<IMyService>() .ImplementedBy<MyServiceImpl&g ...

  6. java访问控制---java编程语言

  7. J2EE 领域的一些技术框架结构图

    J2EE 领域的一些技术框架结构图       阿里百川,开启移动应用开发的新篇章 1.Spring 架构图 Spring 是一个开源 框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之 ...

  8. HDU 1255 覆盖的面积 (线段树扫描线+面积交)

    自己YY了一个的写法,不过时间复杂度太高了,网上的想法太6了  题意:给你一些矩阵,求出矩阵的面积并 首先按照x轴离散化线段到线段树上(因为是找连续区间,所以段建树更加好做). 然后我们可以想一下怎样 ...

  9. 兼容IE8及其他浏览器的回车事件

    //阻止默认浏览器行为 function stopDefault(e) { //如果提供了事件对象,则这是一个非IE浏览器 if(e && e.preventDefault) { // ...

  10. Selenium-免登录的实现

    场景: 经常会遇到登录需要验证码这样类似的情况 解决方案: 1.万能验证码 2.利用cookies来实现,问开发哪个cookies值是登录后需要的,把这个值带上即可 3.利用火狐浏览器的profile ...