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.二分求符合条件的最小值 /*================================= ...
随机推荐
- MySQL复制中slave延迟监控
在MySQL复制环境中,我们通常只根据 Seconds_Behind_Master 的值来判断SLAVE的延迟.这么做大部分情况下尚可接受,但并不够准确,而应该考虑更多因素. 首先,我们先看下SLAV ...
- HDU 1260 Tickets(简单dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- redis rdb
http://blog.chinaunix.net/uid-1757778-id-3977331.html
- JSTL Tag学习笔记之<fn: />
在JSTL中也提供了一些标准的函数,但是几乎都是和操作字符串相关的函数,如果需要使用这类函数的话,应该引入下面的taglib: <%@ taglib prefix="fn" ...
- weka平台
weka平台界面简介 纵向排列的四个主要功能 1.探索(写自己的代码) 2.实验(比较算法) 3.可视化 4.命令行 1.探索 先将weka-src.jar文件解压到一个文件夹 将文件夹导入到Elip ...
- CF A. Xenia and Divisors
题目大意: n(为三的倍数)个数的一个序列(每个数均不大于7),找出a,b,c a能被b整除,b能被c整除,序列中的每个数都被用到. 1 2 3 4 5 6 7 只有 1 2 4 1 2 6 1 3 ...
- 关于SIGPIPE导致的程序退出
http://www.cppblog.com/elva/archive/2008/09/10/61544.html 收集一些网上的资料,以便参考: http://blog.chinaunix.net/ ...
- tomcat console
1.大家都知道,在Tomcat5及其以后的版本中,当启动tomcat之后,是看不到控制台中的manager应用的.Manager的应用还是很有好处的,可以直接在控制台上(类似于weblogic上的co ...
- jsp中利用checkbox进行批量删除
一.将前台jsp页面中的所有你要用到checkbox的name值设为相同,如 <input type="checkbox" name="userid"&g ...
- iOS开发--自动布局
距离左边的: 距离顶部的: 距离右边的: 距离底部的: