也是一个走迷宫的问题,不过又有了点变化。

这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的。问人是否能走出迷宫。

我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间。

第二遍便是走迷宫,只有当这个格子不是墙,而且当前时间在这个格子着火之前才能拓展。当然,并不是所有的空格都一定会着火。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. int x, y, t;
  9. Node(int x=, int y=, int t=):x(x), y(y), t(t) {}
  10. };
  11.  
  12. Node st;
  13.  
  14. const int maxn = + ;
  15. int row, col;
  16. char maze[maxn][maxn];
  17. int time[maxn][maxn];
  18. bool vis[maxn][maxn];
  19.  
  20. int dx[] = { , , , - };
  21. int dy[] = { , , -, };
  22.  
  23. inline bool in(int x, int y)
  24. { return x >= && x < row && y >= && y < col; }
  25.  
  26. inline bool border(int x, int y)
  27. { return x == || x == row- || y == || y == col-; }
  28.  
  29. void init()
  30. {
  31. memset(time, -, sizeof(time));
  32. queue<Node> Q;
  33. for(int i = ; i < row; i++)
  34. for(int j = ; j < col; j++)
  35. {
  36. if(maze[i][j] == 'F') { Q.push(Node(i, j, )); time[i][j] = ; }
  37. if(maze[i][j] == 'J') { st.x = i; st.y = j; st.t = ; }
  38. }
  39. while(!Q.empty())
  40. {
  41. Node now = Q.front(); Q.pop();
  42. for(int i = ; i < ; i++)
  43. {
  44. int x = now.x + dx[i];
  45. int y = now.y + dy[i];
  46. int t = now.t + ;
  47. if(in(x, y) && time[x][y] < && maze[x][y] != '#')
  48. {
  49. Q.push(Node(x, y, now.t + ));
  50. time[x][y] = t;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. int BFS()
  57. {
  58. memset(vis, false, sizeof(vis));
  59. queue<Node> Q;
  60. Q.push(st);
  61. vis[st.x][st.y] = true;
  62. while(!Q.empty())
  63. {
  64. Node now = Q.front(); Q.pop();
  65. if(border(now.x, now.y)) return now.t + ;
  66. for(int i = ; i < ; i++)
  67. {
  68. int x = now.x + dx[i];
  69. int y = now.y + dy[i];
  70. int t = now.t + ;
  71. if(in(x, y) && !vis[x][y] && maze[x][y] != '#')
  72. {
  73. if(time[x][y] >= && time[x][y] <= t) continue;
  74. vis[x][y] = true;
  75. Q.push(Node(x, y, t));
  76. }
  77. }
  78. }
  79. return ;
  80. }
  81.  
  82. int main()
  83. {
  84. //freopen("in.txt", "r", stdin);
  85.  
  86. int T; scanf("%d", &T);
  87. while(T--)
  88. {
  89. scanf("%d%d", &row, &col);
  90. for(int i = ; i < row; i++) scanf("%s", maze[i]);
  91. init();
  92. int ans = BFS();
  93. if(ans) printf("%d\n", ans);
  94. else puts("IMPOSSIBLE");
  95. }
  96.  
  97. return ;
  98. }

代码君

UVa 11624 (BFS) Fire!的更多相关文章

  1. UVA - 11624 J - Fire! (BFS)

    题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...

  2. uva 11624(bfs)

    11624 - Fire! Time limit: 1.000 seconds Joe works in a maze. Unfortunately, portions of the maze hav ...

  3. UVA 11624 BFS的妙用

    题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...

  4. 【UVA - 11624】Fire!

    -->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...

  5. (UVA 11624)Fire!

    题目链接 http://vjudge.net/contest/121377#problem/J Joe works in a maze. Unfortunately, portions of the ...

  6. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  7. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  8. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  9. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

随机推荐

  1. sao/jsp

    sao/i18n/message/ Messages-Client.xml   Messages-Server.xml   sao/wsdl Verification.wsdl   IProcessS ...

  2. 在字符串S1中删除字符串S2中所包含的字符

    /************************************************************************* > File Name: test.c &g ...

  3. Using the viewport meta tag to control layout on mobile browsers

    A typical mobile-optimized site contains something like the following: <meta name="viewport& ...

  4. amcharts简单封装

    只是简单是封装了一下,目前只能输出线图(折现,圆滑线等),柱状图. 代码如下: ;!function(win,$,AC,undefined){ var DDcharts = function(o){ ...

  5. 浅谈Spark Kryo serialization

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3833985.html 最近在使用spark开发过程中发现当数据量很大时,如果cache数据将消耗很多的内 ...

  6. centOS学习part1:操作系统安装

    0 linux作为服务器的主要操作系统,在处理速度以及安全性上都要优于windows,虽然需要很多命令要记,但是一般常用的命令不多,用多了就熟悉了,而且现在很多都要图形界面,也降低了学习成本. cen ...

  7. 李洪强经典iOS面试题11

        #import 跟#include 又什么区别,@class呢, #import<> 跟 #import””又什么区别? #import是Objective-C导入头文件的关键字, ...

  8. Android核心分析之十八Android电话系统之RIL-Java

    Android RIL-Java 123.jpg (2.09 KB, 下载次数: 1) 下载附件  保存到相册 2012-3-21 10:47 上传   RIL-Java在本质上就是一个RIL代理,起 ...

  9. mysql+heartbeat+DRBD+LVS集群

  10. ios开发--企业帐号发布

    这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号. 首先详细说明一下我们的目标,我们需要发布一个ipa放到网上,所有人(包括越狱及非越狱设备)可以直接通过链接下载安装,不需要通过 ...