题目链接 http://vjudge.net/contest/121377#problem/J

  1. Joe works in a maze. Unfortunately, portions of the maze have
  2. caught on fire, and the owner of the maze neglected to create a fire
  3. escape plan. Help Joe escape the maze.
  4. Given Joes location in the maze and which squares of the maze
  5. are on fire, you must determine whether Joe can exit the maze before
  6. the fire reaches him, and how fast he can do it.
  7. Joe and the fire each move one square per minute, vertically or
  8. horizontally (not diagonally). The fire spreads all four directions
  9. from each square that is on fire. Joe may exit the maze from any
  10. square that borders the edge of the maze. Neither Joe nor the fire
  11. may enter a square that is occupied by a wall.
  12. Input
  13. The first line of input contains a single integer, the number of test
  14. cases to follow. The first line of each test case contains the two
  15. integers R and C, separated by spaces, with R, C . The
  16. following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
  17. C characters, and each of these characters is one of:
  18. #, a wall
  19. ., a passable square
  20. J, Joes initial position in the maze, which is a passable square
  21. F, a square that is on fire
  22. There will be exactly one J in each test case.
  23. Output
  24. For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the
  25. fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
  26. Sample Input
  27.  
  28. ####
  29. #JF#
  30. #..#
  31. #..#
  1. ###
  2. #J.
  3. #.F
  4. Sample Output
  5.  
  6. IMPOSSIBLE

题意:有个人在一个R*C的迷宫里,迷宫的某些点有火源,以每秒一格的速度向四周扩散,墙过不去,给出人的坐标,走到边缘就算逃出,问这个人是否能成功逃出,如逃出输出需要几秒?如不能输出-1;

方法:先让所有的火向四周扩散,再看这个人是否能逃出去

  1. #include<stdio.h>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<math.h>
  6. #include<queue>
  7. using namespace std;
  8. #define INF 0x3f3f3f3f
  9. #define ll long long
  10. #define met(a,b) memset(a,b,sizeof(a))
  11. #define N 1009
  12. int w[N][N],ww[N][N];
  13. int a[][]={{,},{-,},{,},{,-}};
  14. char str[N][N];
  15. int n,m;
  16. struct node
  17. {
  18. int x,y,s;
  19. };
  20. queue<node> Q;
  21. node e,f,q,p;
  22. void qq()///求火能蔓延的到所能蔓延的点得时间
  23. {
  24. memset(ww,INF,sizeof(ww));
  25. while(Q.size())
  26. {
  27. p=Q.front();Q.pop();
  28. ww[p.x][p.y]=p.s;
  29. for(int i=;i<;i++)
  30. {
  31. q.x=p.x+a[i][];
  32. q.y=p.y+a[i][];
  33. q.s=p.s+;
  34. if(q.x>=&&q.x<n&&q.y>=&&q.y<m&&str[q.x][q.y]!='#'&&!w[q.x][q.y])
  35. {
  36. w[q.x][q.y]=;
  37. Q.push(q);
  38. }
  39. }
  40. }
  41. }
  42. int dfs()///人从起点开始走所有点遍历,看是否是到出口
  43. {
  44. qq();
  45. memset(w,,sizeof(w));
  46. queue<node> o;
  47. o.push(f);
  48. while(o.size())
  49. {
  50. p=o.front();o.pop();
  51. if(p.x==||p.x==n-||p.y==||p.y==m-)
  52. return p.s+;
  53. for(int i=;i<;i++)
  54. {
  55. q.x=p.x+a[i][];
  56. q.y=p.y+a[i][];
  57. q.s=p.s+;
  58. if(q.x>=&&q.x<n&&q.y>=&&q.y<m&&!w[q.x][q.y]
  59. &&str[q.x][q.y]=='.'&&q.s<ww[q.x][q.y])
  60. {
  61. w[q.x][q.y]=;
  62. o.push(q);
  63. }
  64. }
  65. }
  66. return -;
  67. }
  68. int main()
  69. {
  70. int T;
  71. scanf("%d",&T);
  72. while(T--)
  73. {
  74. met(w,);
  75. scanf("%d%d",&n,&m);
  76. for(int i=;i<n;i++)
  77. scanf("%s",str[i]);
  78. for(int i=;i<n;i++)
  79. {
  80. for(int j=;j<m;j++)
  81. {
  82. if(str[i][j]=='F')///不止一个火源,找到一个存一个
  83. {
  84. e.x=i;e.y=j;e.s=;
  85. w[i][j]=;Q.push(e);
  86. }
  87. if(str[i][j]=='J')
  88. {
  89. f.x=i;f.y=j;
  90. f.s=;
  91. }
  92. }
  93. }
  94. int ans;
  95. ans=dfs();
  96. if(ans==-) printf("IMPOSSIBLE\n");
  97. else
  98. printf("%d\n",ans);
  99. }
  100. return ;
  101. }

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

  1. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  2. 并查集(UVA 1106)

    POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检 ...

  3. UVA - 11624 J - Fire! (BFS)

    题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...

  4. L-Gap Substrings(uva 10829)

    题意:有一种形如uvu形式的字符串,其中u是非空字符串,且V的长度正好为L,那么称这个字符串为L-Gap字符串 给出一个字符串S,以及一个正整数L,问S中有多少个L-Gap子串. /* 这道题用到一个 ...

  5. Minimum Sum LCM(uva 10791)

    题意(就是因为读错题意而wa了一次):给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1 ...

  6. Killer Problem (UVA 11898 )

    Problem You are given an array of N integers and Q queries. Each query is a closed interval [l, r]. ...

  7. POJ 2250 Compromise (UVA 531)

    LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...

  8. 【UVA - 11624】Fire!

    -->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...

  9. uva 1639--精度处理方法之取对数(uva 1639)

    1639 - Candy Time limit: 3.000 seconds 1639 CandyLazyChild is a lazy child who likes candy very much ...

随机推荐

  1. Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  2. 奇妙华为3c手机, 出现安装未成功问题。

    1.我会命令adb uninstall 先卸载我们有签名的安装包. 2.用开发模式在手机上执行我们的应用. 3.然后測试就把手机拿走了,就用应用管理器把我们的应用卸载了. 4.继续装我们的开发包,死活 ...

  3. cocos2dx3.2 画图方法小修改之 C++ final学习

    今天用cocos2dx 3.2版本号学习画图功能,       于是我重载Node 的draw方法.发现报错, watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  4. delphi Components[i]清除所有edit控件中的内容

    (* 一般的清空combobox方法 combobox1.clear; ... combobox9.clear; *)   procedure TForm1.Button1Click(Sender: ...

  5. [ES6] 18. Map

    ES6 provides Map, it is a set of k-v pair. Key can be number, string, object, function and even unde ...

  6. [AngularJS] Provider

    This lesson describes what is really happening when you use the angularfactory and how you can make ...

  7. AngularJS - 定时器 倒计时例子

    <body> <div ng-app="myApp"> <div ng-controller="firstController"& ...

  8. 基于MTD的NAND驱动开发、K9F1G08 2K page、Yaffs2 Files System

    转载:http://hi.baidu.com/cui1206/item/1d4119e376132513585dd886 基于MTD的NAND驱动(linux-2.6.22.10内核),目前已可以在该 ...

  9. cocos2d-x lua 实现单例(管理类)

    cocos2d-x lua 实现单例(管理类) version: cocos2d-x 3.6 示例代码 local DogManager = class("DogManager") ...

  10. Android(java)学习笔记69:JDK5之后的Lock锁的概述和使用

    1. Lock锁的概述: java.util.concurrent.locks,接口Lock 首先Lock是一个接口,Lock实现提供了比使用synchronized方法 和 同步代码块更为广泛的锁定 ...