#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. linux 文件存取 软硬联接的区别

    一.linux文件存取过程 在linux系统中根目录是自引用的,比如要找 /etc/sysconfig/networkscripts/ifcfg-0文件 先根据根目录/ 的inode号,在inode ...

  2. BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊:分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...

  3. redis实现session共享,哨兵

    一.Redis介绍 1.redis是key-value的存储系统,属于非关系型数据库 2.特点:支持数据持久化,可以让数据在内存中保存到磁盘里(memcached:数据存在内存里,如果服务重启,数据会 ...

  4. 大数据日志分析产品——SaaS Cloud, e.g. Papertrail, Loggly, Sumo Logic;Open Source Frameworks, e.g. ELK stack, Graylog;Enterprise Products, e.g. TIBCO LogLogic, IBM QRadar, Splunk

    Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brough ...

  5. flex与js交互浅析

    1.flex调用js方法 调用方法例如:ExternalInterface.call("UploadComplete",oldName,uidName,_dir+"/&q ...

  6. hibernate一级缓存和二级缓存的区别(转)

    缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...

  7. 4_Prototype 原型

    #Prototype ``` // 不好的做法 monster ghost demon sorcerer class Spawner { public: virtual ~Spawner() {} ; ...

  8. cudnn 卷积例子

    运行环境:linux cuda cudnn cudnn API:https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index ...

  9. php断点续传

    http://www.cnblogs.com/xproer/archive/2012/10/26/2741264.html

  10. CTS 2019 Pearl

    CTS2019 Pearl 每种颜色有奇偶两种情况,只有偶数和只有奇数个,其对应的的指数型生成函数分别为 \(\frac{e^x+e^{-x}}2,\frac{e^x-e^{-x}}2\) 考虑选出现 ...