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.二分求符合条件的最小值 /*================================= ...
随机推荐
- 如何在帝国cms后台菜单栏中添加删除链接?
下午测试一个网上下载经过二次开发的帝国cms系统,还原完数据进入后台ytkah发现菜单栏多出了几个链接,有点不习惯,就想着怎么把它去掉.由于自己用的win7系统,搜索时没有像xp那样可以搜索包含某字符 ...
- eclipse 自动 注释
在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...
- MM1排队系统
#coding=utf-8 import time import random as rd #import math import pylab as pl def simulate(nameda,u) ...
- 利用MariaDB Galera Cluster实现mariadb的多主复制
一.MariaDB Galera Cluster概要: .简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的 ...
- jquery ajax post 传递数组 ,多checkbox 取值
jquery ajax post 传递数组 ,多checkbox 取值 http://w8700569.iteye.com/blog/1954396 使用$.each(function(){});可以 ...
- SQL Server Configuration Manager出错
在 Windows 桌面上,单击“开始”,然后单击“运行”. 在“打开”框中,键入 MMC,然后单击“确定”. 在“控制台”窗口中,单击菜单栏上的“文件”,然后单击“添加/删除管理单元”. 在“ ...
- 多项式求ln,求exp,开方,快速幂 学习总结
按理说Po姐姐三月份来讲课的时候我就应该学了 但是当时觉得比较难加上自己比较懒,所以就QAQ了 现在不得不重新弄一遍了 首先说多项式求ln 设G(x)=lnF(x) 我们两边求导可以得到G'(x)=F ...
- 10 signs you’re dating the wrong person
10 signs you’re dating the wrong person10个迹象表明TA不是你的真心人 Do you have any exes who were so awful ...
- win7 安装Redis
1.下载Redis的压缩包 https://github.com/dmajkic/redis/downloads 我下载的是redis-2.4.5-win32-win64.zip 下载完后将其解压放在 ...
- awk除去重复行
awk去除重复行,思路是以每一行的$0为key,创建一个hash数组,后续碰到的行,如果数组里已经有了,就不再print了,否则将其print 测试文件: 用awk: 用sort+uniq好像出错了: ...