之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次。

那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间)。

为什么呢?我们重复走过一个点只有一个可能,那就是为了去取那个,所以如果取完后再回头经过这个点的时候剩余时间变多了,我们的目的就达到了。

left数组初值为0

优化:

重置时间的装置最多取一次就够了,所以可以再开一个标记数组vis记录装置是否用过。

  1. //#define LOCAL
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. struct Point
  8. {
  9. int x, y;
  10. int steps;
  11. int time;
  12. }start, end;
  13.  
  14. int map[][], vis[][], left[][];
  15. int dir[][] = {{, }, {-, }, {, }, {, -}};
  16. int row, col;
  17.  
  18. void BFS(void)
  19. {
  20. queue<Point> qu;
  21. start.time = , start.steps = ;
  22. qu.push(start);
  23. while(!qu.empty())
  24. {
  25. Point fir = qu.front();
  26. qu.pop();
  27. if(fir.time == ) continue;
  28. if(fir.x == end.x && fir.y == end.y)
  29. {
  30. printf("%d\n", fir.steps);
  31. return;
  32. }
  33. if(map[fir.x][fir.y] == )
  34. fir.time = ;
  35. for(int i = ; i < ; ++i)
  36. {
  37. int xx = fir.x + dir[i][];
  38. int yy = fir.y + dir[i][];
  39. if(xx< | xx>=row | yy< | yy>=col | (!map[xx][yy]))
  40. continue;
  41. Point next;
  42. next.x = xx, next.y = yy;
  43. next.time = fir.time - ;
  44. next.steps = fir.steps + ;
  45. if(map[xx][yy] == && vis[xx][yy])
  46. continue;
  47. if(next.time > left[xx][yy])
  48. {
  49. left[xx][yy] = next.time;
  50. qu.push(next);
  51. }
  52. }
  53. }
  54. printf("-1\n");
  55. }
  56.  
  57. int main(void)
  58. {
  59. #ifdef LOCAL
  60. freopen("1072in.txt", "r", stdin);
  61. #endif
  62.  
  63. int T;
  64. scanf("%d", &T);
  65. while(T--)
  66. {
  67. scanf("%d%d", &row, &col);
  68. for(int i = ; i < row; ++i)
  69. for(int j = ; j < col; ++j)
  70. {
  71. scanf("%d", &map[i][j]);
  72. if(map[i][j] == )
  73. start.x = i, start.y = j;
  74. if(map[i][j] == )
  75. end.x = i, end.y = j;
  76. }
  77. memset(vis, , sizeof(vis));
  78. memset(left, , sizeof(left));
  79. BFS();
  80. }
  81. return ;
  82. }

代码君

HDU 1072 (不一样的入队条件) Nightmare的更多相关文章

  1. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  2. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  3. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  4. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  5. HDU 1072 Nightmare 题解

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  6. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  7. hdu 1072(BFS) 有炸弹

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意是在一个n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器. 定 ...

  8. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  9. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

随机推荐

  1. 如何在帝国cms后台菜单栏中添加删除链接?

    下午测试一个网上下载经过二次开发的帝国cms系统,还原完数据进入后台ytkah发现菜单栏多出了几个链接,有点不习惯,就想着怎么把它去掉.由于自己用的win7系统,搜索时没有像xp那样可以搜索包含某字符 ...

  2. eclipse 自动 注释

    在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...

  3. MM1排队系统

    #coding=utf-8 import time import random as rd #import math import pylab as pl def simulate(nameda,u) ...

  4. 利用MariaDB Galera Cluster实现mariadb的多主复制

    一.MariaDB Galera Cluster概要: .简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的 ...

  5. jquery ajax post 传递数组 ,多checkbox 取值

    jquery ajax post 传递数组 ,多checkbox 取值 http://w8700569.iteye.com/blog/1954396 使用$.each(function(){});可以 ...

  6. SQL Server Configuration Manager出错

    在 Windows 桌面上,单击“开始”,然后单击“运行”.  在“打开”框中,键入 MMC,然后单击“确定”.  在“控制台”窗口中,单击菜单栏上的“文件”,然后单击“添加/删除管理单元”.  在“ ...

  7. 多项式求ln,求exp,开方,快速幂 学习总结

    按理说Po姐姐三月份来讲课的时候我就应该学了 但是当时觉得比较难加上自己比较懒,所以就QAQ了 现在不得不重新弄一遍了 首先说多项式求ln 设G(x)=lnF(x) 我们两边求导可以得到G'(x)=F ...

  8. 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 ...

  9. win7 安装Redis

    1.下载Redis的压缩包 https://github.com/dmajkic/redis/downloads 我下载的是redis-2.4.5-win32-win64.zip 下载完后将其解压放在 ...

  10. awk除去重复行

    awk去除重复行,思路是以每一行的$0为key,创建一个hash数组,后续碰到的行,如果数组里已经有了,就不再print了,否则将其print 测试文件: 用awk: 用sort+uniq好像出错了: ...