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

双向BFS。这题会被样例误导比较坑。。F点其实可以有多个。把J和someF分别加入两个队列,先扩展F点,把一步之内的点标记下,再扩展J,使得F可以影响J的路线。当J到达边界即逃脱,否则被#墙及F点围堵Fire。。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. char a[][];
  7. int b[][];
  8. int t[][]={{,},{,},{-,},{,-}};
  9.  
  10. struct Node{
  11. int x,y,s;
  12. }node;
  13.  
  14. int main()
  15. {
  16. int tt,n,m,i,j;
  17. scanf("%d",&tt);
  18. while(tt--){
  19. scanf("%d%d",&n,&m);
  20. queue<Node> qj,qf;
  21. memset(a,,sizeof(a));
  22. memset(b,,sizeof(b));
  23. for(i=;i<n;i++){
  24. getchar();
  25. scanf("%s",a[i]);
  26. for(j=;j<m;j++){
  27. if(a[i][j]=='J'){
  28. b[i][j]=;
  29. node.x=i;
  30. node.y=j;
  31. node.s=;
  32. qj.push(node);
  33. }
  34. if(a[i][j]=='F'){
  35. b[i][j]=;
  36. node.x=i;
  37. node.y=j;
  38. node.s=;
  39. qf.push(node);
  40. }
  41. }
  42. }
  43. int f=;
  44. while(qj.size()){
  45. int ss=qf.front().s;
  46. while(qf.size()&&ss==qf.front().s){
  47. for(i=;i<;i++){
  48. int tx=qf.front().x+t[i][];
  49. int ty=qf.front().y+t[i][];
  50. if(tx<||ty<||tx>=n||ty>=m) continue;
  51. if(a[tx][ty]=='#') continue;
  52. if(b[tx][ty]==){
  53. b[tx][ty]=;
  54. node.x=tx;
  55. node.y=ty;
  56. node.s=qf.front().s+;
  57. qf.push(node);
  58. }
  59. }
  60. qf.pop();
  61. }
  62. int sss=qj.front().s;
  63. while(qj.size()&&sss==qj.front().s){
  64. for(i=;i<;i++){
  65. int tx=qj.front().x+t[i][];
  66. int ty=qj.front().y+t[i][];
  67. if(tx<||ty<||tx>=n||ty>=m){
  68. f=qj.front().s+;
  69. break;
  70. }
  71. if(a[tx][ty]=='#') continue;
  72. if(b[tx][ty]==){
  73. b[tx][ty]=;
  74. node.x=tx;
  75. node.y=ty;
  76. node.s=qj.front().s+;
  77. qj.push(node);
  78. }
  79. }
  80. if(f!=) break;
  81. qj.pop();
  82. }
  83. if(f!=) break;
  84. }
  85. if(f==) printf("IMPOSSIBLE\n");
  86. else printf("%d\n",f);
  87. }
  88. return ;
  89. }

UVA - 11624 Fire! 双向BFS追击问题的更多相关文章

  1. UVA 11624 - Fire! 图BFS

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

  2. UVa 11624 Fire!(BFS)

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

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

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

  4. UVA - 11624 Fire! 【BFS】

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

  5. uva 11624 Fire! 【 BFS 】

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

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

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

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

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

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

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

  9. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

随机推荐

  1. ubuntu 12.04改变源(转载)

    来源:http://blog.ubuntusoft.com/ubuntu-update-source.html#.Uq_PP9KBmxh 其它版本的修改方式相识.尽量使用原生工具来修改(见下方). 手 ...

  2. SVN经常使用命令总结(持续更新)

    如今流行的协同管理工具预计就属SVN和Git了.这两者都使用过,只是如今正在使用的是SVN.故将常常使用的命令总结下来. 无论是Windows端的svnclient还是eclipse的subversi ...

  3. CSDN第一期总结之三:Thread的问题(转)

    C#是一门支持多线程的语言,因此线程的使用也是比较常见的.由于线程的知识在Win32编程的时候已经说得过多,所以在.Net中很少介绍这部分(可能.Net不觉得这部分是它所特有的). 那么线程相关的问题 ...

  4. 最简单的Windows程序

    准备研究一下vmp 保护,从一个最简单的Windows程序入手似乎是个不错的想法. 如何才最简单呢,仅仅有一个MessageBox 调用好了. 弹出消息.退出,哦也,够简单吧. 祭出法器VC2010. ...

  5. bapi_goodsmvt_create-GR

    BAPI_GOODSMVT_CREATE 使用方法 * GMCODE Table T158G - - MB01 - Goods Receipts for Purchase Order * - MB31 ...

  6. mysql错误指令:Failed to open file "file_name" error 2/error 22

    网上说Linux下可能会有此问题,及导入sql文件时出现如标题所示的错误.而我用的是windows系统,也出现了同样的问题. source   filename | \. filename 执行这条语 ...

  7. android通过DialogFragment实现时间选择

    在android开发中,时间控件是不可或缺的一部分,特别是在设置个人生日或按时间进行搜索时都要用到.Android有内置的DatePicker和timePicker,使用起来也是相当的方便,既可以在布 ...

  8. HttpServlet cannot be resolved to a type解决方法

    1:是因为没有加入servlet-api.jar 2:下载网址:http://download.csdn.net/detail/jiuyueguang/5745209 3:然后在项目右键->bu ...

  9. Ubuntu 下安装Source Insight [转]

    本文转载自:http://blog.csdn.net/yunfeiyang62/article/details/46662633 安装Source Insight之前需要先安装Wine,然后用Wine ...

  10. CSU - 1530 Gold Rush —— 二进制

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1530 对于一块2^n质量的gold.需要把它分成a质量和b质量(a+b=2^n),且 ...