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 ...
随机推荐
- phpexcel 导入超过26列时的解决方案
$highestColumn = $sheet->getHighestColumn(); // 取得总列数 ++$highestColumn; for ($row = 5; $row <= ...
- ./configure: error: the HTTP rewrite module requires the PCRE library解决
./configure: error: the HTTP rewrite module requires the PCRE library解决 有时候,我们需要单独安装nginx,来处理大量的下载 ...
- cakePHP模型内置回调函数afterFind()的使用。
在用find获取数据后,我们要对所获取到的数据做一些处理,这时,直接在模型层覆盖cakephp内置的回调函数,使用find时会自动调用. 其中$baomings 就是find 到的 $this-> ...
- Webpack Loaders
[Webpack Loaders] 1.Query parameters Loader can be passed query parameters via a query string (just ...
- python 任务计划
sched 模块 引用time类实现任务定时执行 import time import sched def worker(msg): print msg s = sched.scheduler ...
- spket插件安装并设置JQuery自动提示(转)
spket是一个开发JavaScript.jQuery.Ext_js等的开发工具,它可以 是独立的IDE,也可以作为Eclipse的插件使用,下面介绍如何在Eclipse中安装spket插件: 1.首 ...
- C library:<cctype>(ctype.h)
1, isalnum(): check whether c is either a decimal digit or an uppercase or lowercase letter. 2, isal ...
- 从尾到头打印链表(python)
题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, ...
- Attention Please
关于BJJ与Matlab的学习时间安排在五六日晚间: 其余一切重心在学术!
- XMPP openfire Smack 即时通讯
重新整理下这篇文章. 这篇文章的主要任务是使用AndroidStudio,通过Openfire,利用XMPP协议完成一个可以即时通讯.拥有好友系统的聊天软件. 一.服务器配置与相关库 理论不多说,只谈 ...