hdu 1072(BFS) 有炸弹
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) 有炸弹的更多相关文章
- HDU 1072/BFS
题目链接 Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- hdu - 1072(dfs剪枝或bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- hdu 1072 有炸弹的迷宫 (DFS)
题意:在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间不 ...
- HDU 1072 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
随机推荐
- effective C++学习一(仅供个人学习记录,本文摘录effective C++)
条款 1:尽量用 const 和 inline 而不用#define #define ASPECT_RATIO 1.653 编译器会永远也看不到 ASPECT_RATIO 这个符号名,因为在源码进 ...
- jquery iframe父子框架中的元素访问方法
在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素 1. 格式:window. ...
- yii基础控制器安全验证
- gdb 常用调试命令
1. file quit 2. frame bt 3. finish 运行程序,直到当前函数完成返回,并打印函数返回时的堆栈地址和返回值及参数信息. until 当要退出在一个循环体 ...
- frambuffer 相关函数理解
1. framebuffer_alloc()功能是向内核申请一段大小为sizeof(struct fb_info) + sizeprivate的空间,其中sizeprivate的大小代表设备的私有数据 ...
- struts2漏洞信息
渗透篇01-struts2漏洞利用 https://blog.csdn.net/qq_38055050/article/details/79841604 Struts2著名RCE漏洞引发的十年之思 ...
- ABAP表控件查询
1.准备工作 首先SE11自建一个数据库表(数据元素,域信息请提前建好) 2.编写代码 2.1 新建一个子屏幕 子屏幕中需新定义一个文本输入框,命名为:key_word,新建一个表控件,命名为tab, ...
- Java方法的重载和重写
重载与重写对比: 重载: 权限修饰符(public private 默认):无关 方法名:重载的两个方法的方法名必须相同 形参的个数不同 形参的类型不同 三者至少满足一个 返回值类型: 重载与返回值 ...
- linux 升级python2.7
linux为centos6,系统默认安装了python2.6,需要执行的python脚本内容包含标准库之xml.etree.ElementTree 用到库里的一个iter方法是python2.7的新 ...
- artTemplate js模板引擎动态给html赋值
html放到$("#area").append(html);之前,否则文档流获取不到#area <table width="90%" class=&quo ...