题意

有一个人 有一些火

人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延

求 人能不能跑出来 如果能 求最小时间

思路

有一个 坑点 火是 可能有 多处 的 样例中 只有一处

然后 先让 火 蔓延 再让人走

BFS

AC代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <climits>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <deque>
  11. #include <vector>
  12. #include <queue>
  13. #include <string>
  14. #include <map>
  15. #include <stack>
  16. #include <set>
  17. #include <numeric>
  18. #include <sstream>
  19. #include <iomanip>
  20. #include <limits>
  21. #define CLR(a) memset(a, 0, sizeof(a))
  22. #define pb push_back
  23. using namespace std;
  24. typedef long long ll;
  25. typedef long double ld;
  26. typedef unsigned long long ull;
  27. typedef pair <int, int> pii;
  28. typedef pair <ll, ll> pll;
  29. typedef pair<string, int> psi;
  30. typedef pair<string, string> pss;
  31. const double PI = acos(-1);
  32. const double E = exp(1);
  33. const double eps = 1e-30;
  34. const int INF = 0x3f3f3f3f;
  35. const int maxn = 1e3 + 5;
  36. const int MOD = 1e9 + 7;
  37. string G[maxn];
  38. int Move[8][2]
  39. {
  40. -1, 0,
  41. 1, 0,
  42. 0,-1,
  43. 0, 1,
  44. -1, 1,
  45. -1,-1,
  46. 1, 1,
  47. 1,-1,
  48. };
  49. int n, m;
  50. int ans;
  51. bool ok(int x, int y)
  52. {
  53. if (x < 0 || x >= n || y < 0 || y >= m || G[x][y] != '.' )
  54. return false;
  55. return true;
  56. }
  57. bool edge(int x, int y)
  58. {
  59. if (x == 0 || x == n - 1 || y == 0 || y == m - 1)
  60. return true;
  61. return false;
  62. }
  63. struct Node
  64. {
  65. int x, y, step;
  66. }tmp;
  67. queue <Node> fire, q;
  68. void bfs()
  69. {
  70. int len = fire.size();
  71. for (int i = 0; i < len; i++)
  72. {
  73. int x = fire.front().x;
  74. int y = fire.front().y;
  75. fire.pop();
  76. for (int j = 0; j < 4; j++)
  77. {
  78. tmp.x = x + Move[j][0];
  79. tmp.y = y + Move[j][1];
  80. if (ok(tmp.x, tmp.y))
  81. {
  82. G[tmp.x][tmp.y] = 'F';
  83. fire.push(tmp);
  84. }
  85. }
  86. }
  87. len = q.size();
  88. for (int i = 0; i < len; i++)
  89. {
  90. int x = q.front().x;
  91. int y = q.front().y;
  92. int step = q.front().step;
  93. q.pop();
  94. if (edge(x, y))
  95. {
  96. ans = step;
  97. return;
  98. }
  99. for (int j = 0; j < 4; j++)
  100. {
  101. tmp.x = x + Move[j][0];
  102. tmp.y = y + Move[j][1];
  103. if (ok(tmp.x, tmp.y))
  104. {
  105. tmp.step = step + 1;
  106. G[tmp.x][tmp.y] = '*';
  107. q.push(tmp);
  108. }
  109. }
  110. }
  111. if (q.size())
  112. bfs();
  113. }
  114. int main()
  115. {
  116. int t;
  117. cin >> t;
  118. while (t--)
  119. {
  120. while (!fire.empty())
  121. fire.pop();
  122. while (!q.empty())
  123. q.pop();
  124. scanf("%d%d", &n, &m);
  125. for (int i = 0; i < n; i++)
  126. {
  127. cin >> G[i];
  128. for (int j = 0; j < m; j++)
  129. {
  130. if (G[i][j] == 'J')
  131. {
  132. tmp.x = i;
  133. tmp.y = j;
  134. tmp.step = 0;
  135. q.push(tmp);
  136. G[i][j] = '.';
  137. }
  138. else if (G[i][j] == 'F')
  139. {
  140. tmp.x = i;
  141. tmp.y = j;
  142. fire.push(tmp);
  143. }
  144. }
  145. }
  146. ans = -1;
  147. bfs();
  148. if (ans == -1)
  149. printf("IMPOSSIBLE\n");
  150. else
  151. printf("%d\n", ans + 1);
  152. }
  153. }

UVA - 11624 Fire! 【BFS】的更多相关文章

  1. uva 11624 Fire! 【 BFS 】

    按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...

  2. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  3. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  4. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

  5. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

  6. UVA_11624 Fire! 【BFS】

    一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...

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

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

  8. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  9. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

随机推荐

  1. Flutter开发记录part1

    (1)AppBar:automaticallyImplyLeading//是否带返回leading箭头 (2)非route路由页面跳转 :Navigator.of(context).push(Mate ...

  2. 利用js实现table增加一行

    简单的方法: 用jquery插件,比如设置该table的id为mytable <table id="mytable"> <tr> <td> 第一 ...

  3. oracle如何获得新插入记录的id

    .对于提交(最后一次操作commit了)的话可以查询那个提交段 SELECT 列名1,列名2…… FROM 表名 VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAX ...

  4. JAVA_StandardServer await create[8005]怎么办

    Tomcat 6.0 错误信息: 严重: StandardServer.await:create[8005]: java.net.BindException: Address already in u ...

  5. eletron 播放rtmp flash 播放器问题

    1 安装 flash https://www.flash.cn/ 2 man.js 配置 参考 https://newsn.net/say/electron-flash-win.html 3 播放器 ...

  6. WeakReference &&reference quene &&GC

    在了解WeakReference之前,先给出一段简单的代码: public class WeakReferenceTest {public static void main(String[] args ...

  7. vue Object.freeze() 优化

    参考自:https://segmentfault.com/a/1190000006191558 Object.freeze()是ES5新增的特性,可以冻结一个对象,防止对象被修改. vue 1.0.1 ...

  8. 常用global.css

    html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img { margin: ...

  9. erlang中遍历取出某个位置的最大值

    例:有这么一个列表,A = [["abc","bds",3],["ssdss","dddx",2],["sfa ...

  10. PythonCookBook笔记——函数

    函数 可接受任意数量参数的函数 接受任意数量的位置参数,使用*参数. 接受任意数量的关键字参数,使用**参数. 只接受关键字参数的函数 强制关键字参数放在某个参数后或直接单个之后. 给函数参数增加元信 ...