按白书上说的,先用一次bfs,求出每个点起火的时间

再bfs一次求出是否能够走出迷宫

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<vector>
  7. using namespace std;
  8.  
  9. const int maxn = ;
  10. const int INF = << -;
  11. int r,c;//r行,c列
  12. int d[maxn][maxn];
  13. int vis[maxn][maxn];
  14. char g[maxn][maxn];
  15. int sx,sy;
  16.  
  17. int dir[][] = {,,-,,,,,-};
  18.  
  19. struct node{
  20. int x,y;
  21. int step;
  22. }p[maxn];
  23.  
  24. queue<node> Q;
  25.  
  26. void bfs1(){
  27. memset(vis,,sizeof(vis));
  28. while(!Q.empty()){
  29. node v = Q.front();Q.pop();
  30. for(int i = ;i < ;i++){
  31. int xx = v.x + dir[i][];
  32. int yy = v.y + dir[i][];
  33. if(xx < || xx > r || yy < || yy > c || vis[xx][yy] || g[xx][yy] != '.') continue;
  34. vis[xx][yy] = ;
  35. d[xx][yy] = min(d[xx][yy],v.step+);
  36. Q.push(node{xx,yy,v.step+});
  37. }
  38. }
  39. }
  40.  
  41. void bfs2(){
  42. queue<node> q;
  43. memset(vis,,sizeof(vis));
  44. q.push(node{sx,sy,});vis[sx][sy] = ;
  45.  
  46. while(!q.empty()){
  47. node u = q.front();q.pop();
  48. if(u.x == r || u.y == c || u.x == || u.y == ){
  49. printf("%d\n",u.step + );
  50. return;
  51. }
  52. for(int i = ;i < ;i++){
  53. int xx = u.x + dir[i][];
  54. int yy = u.y + dir[i][];
  55. if(xx < || xx > r || yy < || yy > c || vis[xx][yy] || g[xx][yy] != '.') continue;
  56. if(u.step + >= d[xx][yy]) continue;
  57. vis[xx][yy] = ;
  58. q.push(node{xx,yy,u.step+});
  59. }
  60. }
  61. puts("IMPOSSIBLE");
  62. }
  63.  
  64. int main(){
  65. int T;
  66. scanf("%d",&T);
  67. while(T--){
  68. while(!Q.empty()) Q.pop();
  69. scanf("%d %d",&r,&c);
  70. for(int i = ;i <= r;i++){
  71. for(int j = ;j <= c;j++) d[i][j] = INF;
  72. }
  73. for(int i = ;i <= r;i++){
  74. for(int j = ;j <= c;j++) {
  75. cin>>g[i][j];
  76. if(g[i][j] == 'F') Q.push(node{i,j,});
  77. if(g[i][j] == 'J') sx = i,sy = j;
  78. }
  79. }
  80. bfs1();
  81. bfs2();
  82. }
  83. return ;
  84. }

加油~~~gooooooo~~

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

  1. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

  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. python编写简单的html登陆页面(3)

    1  在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将静态分配数据,建立表格,存放学生信息 2  加载到静态数据 3  html的编写直接在表格里添加一组数据就行了 4  V ...

  2. 如何在IE11中设置兼容模式?设置的具体方法

    IE11浏览器软件版本:简体中文正式版 For Win7网络工具立即查看 1.同样进入需要兼容性模式的网站,点击菜单栏位工具--F12开发者人员工具!如下图所示. 2.在开发者选项左下侧菜单栏位,点击 ...

  3. day27-1 numpy模块

    目录 numpy array 一维数组 二维数组(用的最多) np.array和list的区别 获取多维数组的行和列 多维数组的索引 高级功能 多维数组的元素替换 多维数组的合并 通过函数方法创建多维 ...

  4. 数据结构(3) 第三天 栈的应用:就近匹配/中缀表达式转后缀表达式 、树/二叉树的概念、二叉树的递归与非递归遍历(DLR LDR LRD)、递归求叶子节点数目/二叉树高度/二叉树拷贝和释放

    01 上节课回顾 受限的线性表 栈和队列的链式存储其实就是链表 但是不能任意操作 所以叫受限的线性表 02 栈的应用_就近匹配 案例1就近匹配: #include <stdio.h> in ...

  5. 在小程序中实现全局混入,以混入的形式扩展小程序的api

    GitHub: https://github.com/WozHuang/mp-extend 相关文章: 小程序全局状态管理,在页面中获取globalData和使用globalSetData 通过页面预 ...

  6. BZOJ 2716/2648 SJY摆棋子 (三维偏序CDQ+树状数组)

    题目大意: 洛谷传送门 这明明是一道KD-Tree,CDQ分治是TLE的做法 化简式子,$|x1-x2|-|y1-y2|=(x1+y1)-(x2+y2)$ 而$CDQ$分治只能解决$x1 \leq x ...

  7. [POJ 3621] Sighting Cows

    01分数规划的基本裸题. 因为路线一定是个环,所以找个最优比率生成环即可 二分一个比值,check一下即可. #include <queue> #include <cstdio> ...

  8. JavaScript基础简介

    JavaScript引入的方式 直接在<script>标签中写 <script> console.log('hello world!'); </script> 引入 ...

  9. TensorFlow 制作自己的TFRecord数据集

    官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而关于 ten ...

  10. ioremap映射函数

    一.ioremap() 函数 Linux在io.h头文件中声明了函数ioremap(),用来将I/O内存资源的物理地址映射到核心虚地址空间(3GB-4GB)中(这里是内核空间),原型如下: 1.ior ...