题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到

之前逃出迷宫,若能逃出输出最短时间。很明显的bfs。但由于火到达的地方人不能抵达,故需先对火进行bfs,标记后若人在火烧到之前抵达即可。最后逃出时间需要加一,

因为当时只是抵达边界,若逃出时间需加一。

  1. #include <stdio.h>
  2. #include <queue>
  3. #include <string.h>
  4. #include <algorithm>
  5. using namespace std;
  6. #define oo 0x3f3f3f3f
  7. int dir[][] = {{, }, {, -}, {, }, {-, }};
  8. int m, n, time[][], vis[][];
  9. char maps[][];
  10. struct point
  11. {
  12. int x, y, step;
  13. };
  14. point start;
  15. queue<point>Fire;
  16. void Fire_time()
  17. {
  18. point now, next;
  19. while(Fire.size())
  20. {
  21. now = Fire.front();
  22. Fire.pop();
  23. time[now.x][now.y] = now.step;
  24. for(int i=; i<; i++)
  25. {
  26. next.x = now.x + dir[i][];
  27. next.y = now.y + dir[i][];
  28. next.step = now.step + ;
  29. if(next.x>= && next.x<m && next.y>= && next.y<n && !vis[next.x][next.y]
  30. && maps[next.x][next.y]!='#')
  31. {
  32. vis[next.x][next.y] = ;
  33. Fire.push(next);
  34. }
  35. }
  36. }
  37. }
  38. int bfs()
  39. {
  40. queue<point>Q;
  41. Q.push(start);
  42. memset(time, oo, sizeof(time));
  43. Fire_time();
  44. memset(vis, , sizeof(vis));
  45. point now, next;
  46. while(Q.size())
  47. {
  48. now = Q.front();
  49. Q.pop();
  50. if(now.x==m- || now.y==n- || now.x== || now.y==)return now.step+;
  51. for(int i=; i<; i++)
  52. {
  53. next.x = now.x + dir[i][];
  54. next.y = now.y + dir[i][];
  55. next.step = now.step + ;
  56. if(next.x>= && next.x<m && next.y>= && next.y<n && !vis[next.x][next.y]
  57. && maps[next.x][next.y]=='.' && next.step<time[next.x][next.y])
  58. {
  59. vis[next.x][next.y] = ;
  60. Q.push(next);
  61. }
  62. }
  63. }
  64. return -;
  65. }
  66. int main()
  67. {
  68. int Text;
  69. scanf("%d", &Text);
  70. while(Text--)
  71. {
  72. scanf("%d %d", &m, &n);
  73. memset(vis, , sizeof(vis));
  74. for(int i=; i<m; i++)
  75. scanf("%s", maps[i]);
  76. for(int i=; i<m; i++)
  77. for(int j=; j<n; j++)
  78. {
  79. if(maps[i][j]=='J')
  80. {
  81. start.x = i;
  82. start.y = j;
  83. start.step = ;
  84. }
  85. if(maps[i][j]=='F')
  86. {
  87. point s;
  88. s.x = i;
  89. s.y = j;
  90. s.step = ;
  91. Fire.push(s);
  92. vis[i][j] = ;
  93. }
  94. }
  95. int ans = bfs();
  96. if(ans==-)printf("IMPOSSIBLE\n");
  97. else printf("%d\n", ans);
  98. }
  99. return ;
  100. }

UVA 11624 Fire!(广度优先搜索)的更多相关文章

  1. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

  2. UVA 11624 Fire! BFS搜索

    题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...

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

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

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

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

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

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

  6. UVA 11624 Fire!【两点BFS】

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

  7. UVA 11624 - Fire! 图BFS

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

  8. UVa 11624 Fire!(BFS)

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

  9. UVA 11624 Fire! (bfs)

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

随机推荐

  1. ef执行记录原生态代码方法。

    select e; var f = (System.Data.Objects.ObjectQuery<SimpleEntry>)final; var s = f.ToTraceString ...

  2. Spring AOP 简单理解

    AOP技术即(面向切面编程)技术是在面向对象编程基础上的发展,AOP技术是对所有对象或一类对象编程.核心是在不增加代码的基础上,还增加了新的功能.AOP编程在开发框架本身用的比较多,而实际项目中,用的 ...

  3. python学习-day14:集合,函数,格式化

    一.集合 定义:由不同元素组成的集合.集合是一组无序排列的可hash值, 可以作为字典的key.元素必须是不可变类型:只能存放数字,字符串,字典 特性:集合的目的是将不同的值放在一起,不同的集合之间可 ...

  4. Gson处理

    public class GsonTools { public GsonTools(){} public static <T> T getPerson(String jsonString, ...

  5. UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)

    UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...

  6. UML精粹1 - 简介

    Martin的主页 http://martinfowler.com/. Pavel Hruby开发的visio模板,可以用来画UML图: http://phruby.com 简介 统一建模语言UML是 ...

  7. EditText----

    ==============01   editText属性 1.输入法Enter键图标的设置: 软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有normal,a ...

  8. mycat 插入语句导致的一个Dobbo问题

    2017-01-03 11:11:52.621 [com.alib] (): [DUBBO] Send heartbeat to remote channel /121.43.177.8:20192, ...

  9. IIS7 rename application or site

    rename site 比较容易,在IIS里面就可以直接 rename,也可以用 cmd 的方式 1. 打开 cmd 2. cd C:\Windows\System32\inetsrv 3. appc ...

  10. 解决Mac下GDB提示签名错误

    http://blog.csdn.net/powerlly/article/details/30323015