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 ...
随机推荐
- CFDA
cfda数据抓取 1.网站数据是加密的,需要浏览器进行数据解析 2.网址url有js加密 3.PhantomJS无法解析数据, chrome无法获取数据,所有最终选择用Firefox浏览器 impor ...
- js基础-语句
if语句 if(true){ console.log(true) } if(0 == false){ console.log(true) } var a,b; Math.random() 随机生成 0 ...
- 修复回写PR时到料日期重复扣减检验周期的问题:
问题描述: 修复回写PR时到料日期重复扣减检验周期的问题:系统回写的外购半成品PR交货日期未按采购周期回写,从8-10日开始均于10天交期回写,例以下9-5日今天回写的PR,采购周期12天,结果回写到 ...
- JMeter学习(二十六)逻辑控制器(转载)
转载自 http://www.cnblogs.com/yangxia-test JMeter中的Logic Controller用于为Test Plan中的节点添加逻辑控制器. JMeter中的Log ...
- vps vultr centos7 搭建 伟皮恩
vultr 配置 64 bit OS CentOS 7 ×64 20 GB SSD 1 CPU 512MB Memory 500GB Bandwidth √ Enable IPv6 √ ...
- MyEclipse 10 注册码 破解 到期限制
转载 https://blog.csdn.net/oJinSeNianHua1/article/details/69226524 把破解界面中的ACTIVATION_KEY复制到弹出的窗体“Enter ...
- java面试题:Spring
Spring 面试时,最好能结合底层代码说出IOC,AOP或Spring MVC的流程,能说出拦截器的底层. 如果看过Spring的源码,并能结合设计模式表达,是很大的加分项. IOC Q:讲一下IO ...
- vue使用全局element-ui组件
安装loader模块: cnpm install style-loader -D cnpm install css-loader -D cnpm install file-loader -D 安装 ...
- Android 各个版本新特性
一.Android 4.x 新锁屏界面: Android4.0重新设计了锁屏幕UI,下方的解锁虚拟按键向周围发射出微光,轻轻拖动就可以解锁,比原来在UI上确实有很大的进步. 全新Widget排列: 主 ...
- 165. Compare Version Numbers (String)
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 & ...