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.二分求符合条件的最小值 /*================================= ...
随机推荐
- Sqli-labs less 27
Less-27 本关主要考察将union,select和26关过滤掉的字符.此处我们依旧和26关的方式是一样的,只需要将union和select改为大小写混合就可以突破. 示例:127.0.0.1/s ...
- 安装wine qq2012
添加软件源:vi /etc/apt/sources.list deb http://http.kali.org/kali kali main non-free contribdeb-src http: ...
- win7建wifi 热点,附wifi小工具
首先申明:1)以下操作均在管理员身份下操作,其他用户下请亲测. 2)具备无线网卡,并且已经安装好了驱动. 1.打开命令行:输入netsh wlan set hostedn ...
- ubuntu第一次设置root密码
安装ubuntu时,系统让用户创建了一个非root用户,系统启动后使用这个用户,在需要执行超级用户权限的指令时,可以通过sudo来执行.为此我们可以通过这样的方式修改root的密码:dengfei@d ...
- hdu 4155 The Game of 31 博弈论
给出序列,在剩下的卡中选择,谁先拿到大于31的输,搜一下就可以了! 代码如下: #include<cstdio> #include<cstring> ]; ],sum; boo ...
- Google Protocol Buffers简介
什么是 protocol buffers ? Protocol buffers 是一种灵活.高效的序列化结构数据的自动机制--想想XML,但是它更小,更快,更简单.你只需要把你需要怎样结构化你的数据定 ...
- Windows 代码实现关机(直接黑屏)
整理资料的时候发现的以前的代码,本机Win7 x64 Sp1 运行直接关机,黑屏.就是利用RtlAdjustPrivilege函数提权,代码中的注释写的很详细了.用的VS2010写的,直接编译成x64 ...
- spring_150804_controller
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- ubuntu下搭建cocos2dx编程环境-中
上篇文章里讲了在ubuntu下部署cocos2d-x开发环境,这篇文章主要示范在ubuntu下部署cocos2d-x android开发环境.分开写就是因为我看很多文章里都将这两件事情混杂着写 ...
- Android之开发杂记(三)
一.popup 弹出框 在onCreate中创建时异常 Unable to add window -- token null is not valid; is your activity runnin ...