HDU 1072 (不一样的入队条件) Nightmare
之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次。
那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间)。
为什么呢?我们重复走过一个点只有一个可能,那就是为了去取那个,所以如果取完后再回头经过这个点的时候剩余时间变多了,我们的目的就达到了。
left数组初值为0
优化:
重置时间的装置最多取一次就够了,所以可以再开一个标记数组vis记录装置是否用过。
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct Point
{
int x, y;
int steps;
int time;
}start, end; int map[][], vis[][], left[][];
int dir[][] = {{, }, {-, }, {, }, {, -}};
int row, col; void BFS(void)
{
queue<Point> qu;
start.time = , start.steps = ;
qu.push(start);
while(!qu.empty())
{
Point fir = qu.front();
qu.pop();
if(fir.time == ) continue;
if(fir.x == end.x && fir.y == end.y)
{
printf("%d\n", fir.steps);
return;
}
if(map[fir.x][fir.y] == )
fir.time = ;
for(int i = ; i < ; ++i)
{
int xx = fir.x + dir[i][];
int yy = fir.y + dir[i][];
if(xx< | xx>=row | yy< | yy>=col | (!map[xx][yy]))
continue;
Point next;
next.x = xx, next.y = yy;
next.time = fir.time - ;
next.steps = fir.steps + ;
if(map[xx][yy] == && vis[xx][yy])
continue;
if(next.time > left[xx][yy])
{
left[xx][yy] = next.time;
qu.push(next);
}
}
}
printf("-1\n");
} int main(void)
{
#ifdef LOCAL
freopen("1072in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &row, &col);
for(int i = ; i < row; ++i)
for(int j = ; j < col; ++j)
{
scanf("%d", &map[i][j]);
if(map[i][j] == )
start.x = i, start.y = j;
if(map[i][j] == )
end.x = i, end.y = j;
}
memset(vis, , sizeof(vis));
memset(left, , sizeof(left));
BFS();
}
return ;
}
代码君
HDU 1072 (不一样的入队条件) Nightmare的更多相关文章
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- 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 (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- 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 3641 数论 二分求符合条件的最小值数学杂题
http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...
随机推荐
- 从3D Studio Max导入物体 Importing Objects From 3D Studio Max
原地址:http://game.ceeger.com/Manual/HOWTO-ImportObjectMax.html If you make your 3D objects in 3dsMax, ...
- 超快的 FastText
Word2Vec 作者.脸书科学家 Mikolov 文本分类新作 fastText:方法简单,号称并不需要深度学习那样几小时或者几天的训练时间,在普通 CPU 上最快几十秒就可以训练模型,得到不错的结 ...
- 由浅入深了解Thrift之客户端连接池化
一.问题描述 在上一篇<由浅入深了解Thrift之服务模型和序列化机制>文章中,我们已经了解了thrift的基本架构和网络服务模型的优缺点.如今的互联网圈中,RPC服务化的思想如火如荼.我 ...
- cf 403 D
D. Beautiful Pairs of Numbers time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- php怎么获取checkbox复选框的内容?
由于checkbox属性,所有必须把checkbox复选择框的名字设置为一个如果checkbox[],php才能读取,以数据形式,否则不能正确的读取checkbox复选框的值哦. <form n ...
- IOS快速集成下拉上拉刷新
http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5% ...
- 2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)
题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据 ...
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]
引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...
- DAL层与BLL层的设计原则
通用DAL层: 提供一个通用的DAL层的基础框架,其中包括所有实体类的基类,所有DAL类的基类,以及用来在实体类和数据表以及实体类字段和数据表字段之间Mapping的Attributes.此层作为核心 ...
- hdu 4762 Cut the Cake (大数乘法)
猜公式: ans=n/m^(n-1) #include<stdio.h> #include<string.h> struct BigNum { ]; int len; }; i ...