http://acm.hdu.edu.cn/showproblem.php?pid=1072

题目大意是在一个n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器。

定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。

当人走到4时如果炸弹的时间不是0,可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,

成功走出。求人从2走到3的最短时间。

一道典型的搜索,用的bfs,结构体中开一个step表示要求的时间,也就相当于步数,一个time表示炸弹的时间,

注意的是这里其实每个点都可以重复访问,为了求能够到达的最小时间,4的位置如果必须要走的话其实只走一次

一个炸弹点如果走多次,虽然设置了最大限时但是往返消耗了时间得不偿失,好好想想这里

code

 #include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct point {
int x,y;
int time,step;//炸弹时间与步数
};
int dx[]={,-,,};
int dy[]={,,,-};
int n,m;
int visit[][],map[][];
int bfs(int sx,int sy)
{
int i;
memset(visit,,sizeof(visit));
queue<point>Q;
point now,next;
now.x=sx;now.y=sy;
now.time=;
now.step=;
visit[now.x][now.y]=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop();
if (map[now.x][now.y]==)
return now.step;
if (now.time<=)continue;
for (i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if (next.x<||next.x>n||next.y<||next.y>m)continue;
if (map[next.x][next.y]==)continue;
if (visit[next.x][next.y]==)continue;
next.step=now.step+;
next.time=now.time-;
if (map[next.x][next.y]==)
{
next.time=;
visit[next.x][next.y]=;//确保只走一次,标记
}
Q.push(next);
}
}
return -;
}
int main()
{
int t,i,j,sx,sy;
while (~scanf("%d",&t)){
while (t--)
{
scanf("%d %d",&n,&m);
for (i=;i<=n;i++)
{
for (j=;j<=m;j++)
{
scanf("%d",&map[i][j]);
if (map[i][j]==)
{
sx=i;
sy=j;
}
}
}
printf("%d\n",bfs(sx,sy));
}
}
return ;
}

hdu 1072(BFS) 有炸弹的更多相关文章

  1. HDU 1072/BFS

    题目链接 Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  4. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  5. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  6. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  7. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  8. hdu 1072 有炸弹的迷宫 (DFS)

    题意:在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间不 ...

  9. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

随机推荐

  1. WilliamChart各种图表效果实现大全《IT蓝豹》

    WilliamChart各种图表效果实现大全,有水平线条表格,有柱状表格等.由LineFragment,BarFragment,StackedFragment,SandboxFragment几个fra ...

  2. List,Set,Map集合的遍历方法

    List的三种实现:ArrayList(数组)  LinkedList(链表)  Vector(线程安全) List集合遍历方法: List<String> list = new Arra ...

  3. R语言-画柱形图

    barplot()函数 1.柱形图 > sales<-read.csv("citysales.csv",header=TRUE) #读取数据 > barplot( ...

  4. idea 这样 会快点

    最近使用IDEA来开发JAVA应用,对IDEA的界面很有爱,但是缺受不了它的运行速度.每次运行都要编译,所以就有了这边文章   总感觉IDEA的编译速度比eclipse慢,eclipse每次保存都自动 ...

  5. http协议解析过程

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. 基于HTTP协议的客户端/服务器请求响应机制的信息交换过程包含下面几个步骤: 1)     建立连接:客 ...

  6. python遇到的错误

    今天学习文件遇到这个错误. 这是在 text_files\vvvv.txt  之间加一个\ 就可以了,变成  text_files\\vvvv.txt,运行成功

  7. hbase 调试各种报错

    1.master is initializing 怎么都不知道怎么回事,直接从hbase 2.0 换到了 hbase 2.1 2.java.lang.ClassNotFoundException: o ...

  8. Executors与ThreadPoolExecutor

    最近阿里发布的 Java开发手册中强制线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗 ...

  9. linux 根据服务名称批量杀死进程

    ps -ef |grep python |awk '{print $2}'|xargs kill -9

  10. JMeter学习(三十三)测试报告优化(转载)

    转载自 http://www.cnblogs.com/yangxia-test 如果按JMeter默认设置,生成报告如下: 从上图可以看出,结果信息比较简单,对于运行成功的case,还可以将就用着.但 ...