题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间

分析:两遍BFS,然后比较边界

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<map>
  7. #include<queue>
  8. #include<stdlib.h>
  9. #include<string>
  10. #include<set>
  11. using namespace std;
  12. typedef long long LL;
  13. const int maxn=;
  14. const int INF=0x3f3f3f3f;
  15. char s[maxn][maxn];
  16. int mp[maxn][maxn];
  17. int v[maxn][maxn];
  18. int n,m;
  19. struct Point
  20. {
  21. int x,y;
  22. Point() {}
  23. Point(int a,int b)
  24. {
  25. x=a,y=b;
  26. }
  27. } o,t;
  28. queue<Point>q,e;
  29. int dx[]= {,,-,};
  30. int dy[]= {-,,,};
  31. int main()
  32. {
  33. int T;
  34. scanf("%d",&T);
  35. while(T--)
  36. {
  37. scanf("%d%d",&n,&m);
  38. for(int i=; i<=n; ++i)
  39. scanf("%s",s[i]+);
  40. for(int i=; i<=n; ++i)
  41. {
  42. for(int j=; j<=m; ++j)
  43. {
  44. mp[i][j]=v[i][j]=-;
  45. if(s[i][j]=='F')q.push(Point(i,j)),mp[i][j]=;
  46. else if(s[i][j]=='J')e.push(Point(i,j)),v[i][j]=;
  47. }
  48. }
  49. while(!q.empty())
  50. {
  51. o=q.front();
  52. q.pop();
  53. for(int i=; i<; ++i)
  54. {
  55. t.x=o.x+dx[i];
  56. t.y=o.y+dy[i];
  57. if(t.x<||t.x>n||t.y<||t.y>m)continue;
  58. if(s[t.x][t.y]=='#'||mp[t.x][t.y]!=-)continue;
  59. mp[t.x][t.y]=mp[o.x][o.y]+;
  60. q.push(t);
  61. }
  62. }
  63. while(!e.empty())
  64. {
  65. o=e.front();
  66. e.pop();
  67. for(int i=; i<; ++i)
  68. {
  69. t.x=o.x+dx[i];
  70. t.y=o.y+dy[i];
  71. if(t.x<||t.x>n||t.y<||t.y>m)continue;
  72. if(s[t.x][t.y]=='#'||v[t.x][t.y]!=-)continue;
  73. v[t.x][t.y]=v[o.x][o.y]+;
  74. e.push(t);
  75. }
  76. }
  77. int ans=INF;
  78. for(int i=;i<=n;++i)
  79. {
  80. if(v[i][]!=-)
  81. {
  82. if(mp[i][]==-||v[i][]<mp[i][])
  83. ans=min(ans,v[i][]);
  84. }
  85. if(v[i][m]!=-)
  86. {
  87. if(mp[i][m]==-||v[i][m]<mp[i][m])
  88. ans=min(ans,v[i][m]);
  89. }
  90. }
  91. for(int i=;i<=m;++i)
  92. {
  93. if(v[][i]!=-)
  94. {
  95. if(mp[][i]==-||v[][i]<mp[][i])
  96. ans=min(ans,v[][i]);
  97. }
  98. if(v[n][i]!=-)
  99. {
  100. if(mp[n][i]==-||v[n][i]<mp[n][i])
  101. ans=min(ans,v[n][i]);
  102. }
  103. }
  104. if(ans==INF)printf("IMPOSSIBLE\n");
  105. else printf("%d\n",ans+);
  106. }
  107. return ;
  108. }

UVA 11624 Fire! BFS搜索的更多相关文章

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

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

  2. uva 11624 Fire!(搜索)

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

  3. UVA 11624 Fire! (bfs)

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

  4. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

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

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

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

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

  7. UVA 11624 Fire!【两点BFS】

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

  8. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  9. UVA 11624 - Fire! 图BFS

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

随机推荐

  1. python学习笔记9(对文件的操作)

    一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. 二.相关函数 [1].内建函数:open() 提供了初始化输入/输出(I/O)操作的通用接口,成功打开一个文件后会返回一个 ...

  2. 集成“支付宝” -b

    大致步骤 1.与支付宝签约获取相关参数 合作者身份 ID 与安全校验码 key2.下载需要导入的文件,做相应设置3.在自己的项目中集成支付的方法代码 详细步骤 1.获取合作者身份 ID 与安全校验码 ...

  3. org.hibernate.LazyInitializationException

    1.org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.c ...

  4. asp.net单点登录(SSO)解决方案

    前些天一位朋友要我帮忙做一单点登录,其实这个概念早已耳熟能详,但实际应用很少,难得最近轻闲,于是决定通过本文来详细描述一个SSO解决方案,希望对大家有所帮助.SSO的解决方案很多,但搜索结果令人大失所 ...

  5. chardet安装

    1.下载 chardet-2.2.1.tar.gz (md5)   https://pypi.python.org/pypi/chardet#downloads 2.解压至C:\Python27\Li ...

  6. 跨平台Unicode与UTF8互转代码

    参考来源:http://blog.csdn.net/flying8127/article/details/1598521 在原来原基础上,将代码整理,并加强安全性. 并按照WindowsAPI设计, ...

  7. 【BZOJ 3190】 3190: [JLOI2013]赛车 (半平面交)

    3190: [JLOI2013]赛车 Description 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2--gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进ki的位 ...

  8. TCP长连接与短连接的区别

    http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html 1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,se ...

  9. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  10. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...