HDU-1072-Nightmares
这题可以用dfs写,我们记忆化搜索。
我们定义一个step和time数组,分别表示走到这点的最小步数,time表示走到该点炸弹还剩多少时间。
递归边界一是,如果走到该点,时间等于0,我们就返回。
如果走到了终点并且我们这次走到这点的最小步数小于了之前的答案,我们就更新答案并返回。
剪枝,首先越界剪枝,然后我们走到一点,不能步数增加了,并且上一次我们走这点炸弹的剩余时间pre,小于这次的炸弹剩余时间now。
那样的话,我们不如不走,我们还可以知道我们如果走到这点,步数和上一次相同,剩余时间也相同,我们就不必再搜这点,所以有两个等于。
然后我们更新步数和时间,更新之后再来判断,此时我们走到下一个房间,是否能够重置时间,可以就重置。
然后搜向下一个节点。
#include <cstdio>
#include <cstring>
const int INF=0x3f3f3f3f;
int map[10][10],step[10][10],time[10][10];
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m,ans; void dfs(int x,int y)
{
if (time[x][y]<=0)
return;
if (map[x][y]==3) {
if (step[x][y]<ans)
ans=step[x][y];
return ;
}
for (int i=0;i<4;i++) {
int dx=x+d[i][0];
int dy=y+d[i][1];
if (dx>=0&&dy>=0&&dx<n&&dy<m&&map[dx][dy]) {
if (step[x][y]+1>=step[dx][dy]&&time[x][y]-1<=time[dx][dy])
continue;
step[dx][dy]=step[x][y]+1;
time[dx][dy]=time[x][y]-1;
if (map[dx][dy]==4&&time[dx][dy]>0)
time[dx][dy]=6;
dfs(dx,dy);
}
}
} int main()
{
int t,sx,sy;
scanf("%d",&t);
while (t--) {
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++) {
scanf("%d",&map[i][j]);
if (map[i][j]==2)
sx=i,sy=j;
}
}
memset(time,0,sizeof(time));
memset(step,INF,sizeof(step));
ans=INF;
time[sx][sy]=6;
step[sx][sy]=0;
dfs(sx,sy);
printf("%d\n",ans==INF?-1:ans);
}
return 0;
}
HDU-1072-Nightmares的更多相关文章
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- 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 题目大意是在一个n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器. 定 ...
- 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 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
- HDU 1072 (不一样的入队条件) Nightmare
之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...
- hdu 1072 广搜
路径是可以重复走的,但是如果再一次走过时间重置点是没有意义的 #include <iostream> #include <cstdio> #include <cstrin ...
- HDU 1072/BFS
题目链接 Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 1072 有炸弹的迷宫 (DFS)
题意:在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间不 ...
随机推荐
- Spring配置文件xsi:schemaLocation无法解析导致启动失败的解决方案
今天遇到过情况,spring的配置文件在本地读取没有问题,扔到线上服务器运行就报无法解析xml,找了很久问题,发现是因为线上服务器无法上网,导致无法下载相关的xsd文件,没办法不能上网就只有使用本地的 ...
- Centos下安装pip失败或新装
Centos安装pip失败: [root@localhost /]# yum -y install pip已加载插件:fastestmirrorRepodata is over 2 weeks old ...
- JS高级学习历程-17
[正则案例] 1 匹配手机号码
- easyui datagrid编辑时编辑框自动获取焦点
onDblClickCell:function(rowIndex, field, val){//双击单元格监听器 $(this).datagrid('beginEdit',rowIndex);//开启 ...
- GDPR(Cookie处理)
GDPR(Cookie处理) https://www.cnblogs.com/GuZhenYin/p/9154447.html 前言 时间一晃 ASP.NET Core已经迭代到2.1版本了. 迫不及 ...
- dot watch
dot watch+vs code提升asp.net core开发效率 在园子中,已经又前辈介绍过dotnet watch的用法,但是是基于asp.net core 1.0的较老版本来讲解的,在asp ...
- 关于Spring配置文件xml文档的schema约束
最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version=&q ...
- 2017 ACM Arabella Collegiate Programming Contest div2的题,部分题目写个题解
F. Monkeying Around 维护点在多少个线段上 http://codeforces.com/gym/101350/problem/F 题意:有m个笑话,每个笑话的区间是[L, R], ...
- CellSet 遍历
CellSet 结构: 查询MDX: SELECT NON EMPTY {{ {{ {{ {{ {{ AddCalculatedMembers([店铺.店铺ID].[店铺ID].Members)}} ...
- 小G搭积木
A小 G 搭积木文件名 输入文件 输出文件 时间限制 空间限制box.cpp box.in box.out 2s 128MB题目描述小 G 喜欢搭积木.小 G 一共有 n 块积木,并且积木只能竖着一块 ...