题目传送门

J - Fire!

Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

Input

The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R,C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of: • #, a wall • ., a passable square • J, Joe’s initial position in the maze, which is a passable square • F, a square that is on fire There will be exactly one J in each test case.

Output

For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input

2 4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F

Sample Output

3

IMPOSSIBLE

题意:题目很清晰,就是代号J要逃离迷宫,但是在迷宫的一些部分有一些火(fire)会蔓延开来,让你求出最短逃离时间,或者输出IMPOSSIBLE

本来解法我都想到了,就是在有火的地方bfs,计算它蔓延到每个地方的时间,然后人再bfs计算出可行的路径,这里有一个坑就是,火不一定只有一个,在文中是用“portions”,注意这里使用复数.对,这里我没注意到,我一开始还提交了8遍CE,提交错了语言,都是泪啊!!最后改过来后,在提交几次WA后我发现我bfs的结束把m写成了n,无语了………

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7. typedef long long ll;
  8. typedef unsigned long long ull;
  9. #define mod 1000000007
  10. #define INF 0x3f3f3f3f
  11. #define MAX 1005
  12. int n,m;
  13. int sx,sy,fx,fy;
  14. char g[MAX][MAX];
  15. bool vis[MAX][MAX];
  16. int fire[MAX][MAX];
  17. int ans=INF;
  18. int dx[]={,,,-},dy[]={,,-,};
  19. struct mask
  20. {
  21. int x,y,step;
  22. mask(){}
  23. mask(int xx,int yy,int st)
  24. {
  25. x=xx,y=yy,step=st;
  26. }
  27. };
  28. struct fir
  29. {
  30. int x,y,time;
  31. fir(){}
  32. fir(int xx,int yy,int ti)
  33. {
  34. x=xx,y=yy,time=ti;
  35. }
  36. };
  37. queue<mask>q;
  38. queue<fir>fi;
  39. bool check(int a,int b)
  40. {
  41. return <=a&&a<n&&<=b&&b<m&&g[a][b]!='#';
  42. }
  43. //遍历火的蔓延速度
  44. void bfs_fire()
  45. {
  46. while(fi.size())
  47. {
  48. fir tmp=fi.front();fi.pop();
  49. for(int i=;i<;i++)
  50. {
  51. int nx=tmp.x+dx[i];
  52. int ny=tmp.y+dy[i];
  53. if(fire[nx][ny]>tmp.time+&&check(nx,ny))
  54. {//cout<<"ok"<<endl;
  55. fire[nx][ny]=min(fire[nx][ny],tmp.time+);
  56. fi.push(fir(nx,ny,tmp.time+));
  57. }
  58. }
  59. }
  60. }
  61. //遍历人的可行路径
  62. int bfs()
  63. {
  64. memset(vis,false,sizeof(vis));
  65. while(q.size())q.pop();
  66. vis[sx][sy]=true;
  67. q.push(mask(sx,sy,));
  68. while(q.size())
  69. {
  70. mask tmp=q.front();q.pop();
  71. if(tmp.x==n-||tmp.y==m-||tmp.x==||tmp.y==)
  72. {
  73. ans=min(ans,tmp.step);
  74. }
  75. for(int i=;i<;i++)
  76. {
  77. int nx=tmp.x+dx[i];
  78. int ny=tmp.y+dy[i];
  79. if(check(nx,ny)&&tmp.step+<fire[nx][ny]&&!vis[nx][ny])
  80. {
  81. vis[nx][ny]=true;
  82. q.push(mask(nx,ny,tmp.step+));
  83. }
  84. }
  85. }
  86. return ans==INF?-:ans;
  87. }
  88. int main()
  89. {
  90. int T;
  91. scanf("%d",&T);
  92. while(T--)
  93. {
  94. scanf("%d%d",&n,&m);
  95. while(fi.size())fi.pop();
  96. memset(fire,INF,sizeof(fire));
  97. for(int i=;i<n;i++)
  98. {
  99. scanf("%s",&g[i]);
  100. for(int j=;j<m;j++)
  101. {
  102. if(g[i][j]=='J')
  103. {
  104. sx=i,sy=j;
  105. }
  106. if(g[i][j]=='F')
  107. {
  108. fire[i][j]=;//注意这里的火可能不止一个,所以要全部加入
  109. fi.push(fir(i,j,));
  110. }
  111. }
  112. }
  113. ans=INF;
  114. bfs_fire();
  115. /* for(int i=0;i<n;i++){
  116. for(int j=0;j<m;j++)
  117. cout<<fire[i][j]<<" ";
  118. cout<<endl;
  119. }*/
  120. int d=bfs();
  121. if(d==-)
  122. printf("IMPOSSIBLE\n");
  123. else printf("%d\n",d+);
  124. }
  125.  
  126. return ;
  127. }

UVA - 11624 J - Fire! (BFS)的更多相关文章

  1. UVa 11624 Fire!(BFS)

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

  2. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

  3. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  4. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  5. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  6. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

随机推荐

  1. 全栈开发系列学习2——django项目搭建

    项目代码:http://yunpan.cn/cHajgT4HvgHqx (提取码:8350) 配置项目: 1. 首先确保你的机器安装了python和pip,这两种安装比较简单,这里就不说了. 2. 在 ...

  2. 20180308-Python内置方法

    先大致粗略的说一下反射的概念,不是很准确,后续详细讲解: 1. 以字符串的形式,导入模块 2. 以字符串的形式,获取模块内部的函数,并执行 通常我们想在一个模块中导入另外一个模块,则需要通过 impo ...

  3. Android 虚线实现绘制 - DashPathEffect

    前言: 通过view绘制虚实线,采用Android自带API--DashPathEffect.具体使用请参考更多的链接,这里只是讲解. 构造函数 DashPathEffect 的构造函数有两个参数: ...

  4. Java并发(基础知识)—— 阻塞队列和生产者消费者模式

    1.阻塞队列                                                                                        Blocki ...

  5. hdu1231 最长连续子序列和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, N ...

  6. Java Web学习总结(6)Cookie/Session

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 二.会话过程中要解决的一些问题 每个用户在使用浏览器与服务器进行 ...

  7. 早日选择一门自己喜欢的,然后瞄准目标,不达目的誓不罢休。像文章的作者一样成为一名成功的IT人士。

    hawk的奋斗历程. 来自:LinuxForum  :http://www3.linuxforum.net/ 原址:http://www.linuxforum.net/forum/gshowflat. ...

  8. 后端技术杂谈4:Elasticsearch与solr入门实践

    阮一峰:全文搜索引擎 Elasticsearch 入门教程 作者:阮一峰 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://g ...

  9. vue仿追书神器,vue小说项目源码

    vue-reader 一点阅读器!API源自追书神器,免费使用!目前已初步开发完成! Github项目地址:https://github.com/AntonySufer/vue-readle 欢迎is ...

  10. error C2065: ‘__in’ : undeclared identifier

    转自VC错误:http://www.vcerror.com/?p=1307 问题描述: 编译时出现: error C2065: '__in' : undeclared identifier error ...