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

首先对火进行一次bfs,得到着火时间,然后对人进行一次bfs,只允许进入还没有着火的点

注意:出迷宫条件是从任何一墙出去,过墙需要1时间

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. const int maxn=1003;
  6. const int inf=0x3fffffff;
  7. char maz[maxn][maxn];
  8. int time[maxn][maxn];
  9. int dp[maxn][maxn];
  10. int n,m;
  11.  
  12. queue<int> que;
  13. const int dx[8]={0,0,1,-1,1,1,-1,-1};
  14. const int dy[8]={1,-1,0,0,1,-1,1,-1};
  15. struct pnt {
  16. int x,y;
  17. pnt(){x=y=0;}
  18. pnt(int tx,int ty){x=tx,y=ty;}
  19. };
  20. bool in(int x,int y){
  21. return x>=0&&x<n&&y>=0&&y<m;
  22. }
  23. void bfs(){
  24. while(!que.empty()){
  25. int x=que.front()/maxn,y=que.front()%maxn;que.pop();
  26. for(int i=0;i<4;i++){
  27. int tx=x+dx[i],ty=y+dy[i];
  28. if(in(tx,ty)&&maz[tx][ty]!='#'&&time[tx][ty]>time[x][y]+1){
  29. time[tx][ty]=time[x][y]+1;
  30. que.push(tx*maxn+ty);
  31. }
  32. }
  33. }
  34. }
  35. int bfs2(int sx,int sy){
  36. while(!que.empty())que.pop();
  37. dp[sx][sy]=0;
  38. que.push(sx*maxn+sy);
  39. while(!que.empty()){
  40. int x=que.front()/maxn,y=que.front()%maxn;que.pop();
  41. for(int i=0;i<4;i++){
  42. int tx=x+dx[i],ty=y+dy[i];
  43. if(!in(tx,ty)){
  44. return dp[x][y]+1;
  45. }
  46. if(maz[tx][ty]!='#'&&time[tx][ty]>dp[x][y]+1&&dp[tx][ty]>dp[x][y]+1){
  47. dp[tx][ty]=dp[x][y]+1;
  48. que.push(tx*maxn+ty);
  49. }
  50. }
  51. }
  52. return -1;
  53. }
  54. void init(){
  55. while(!que.empty())que.pop();
  56. for(int i=0;i<n;i++){
  57. for(int j=0;j<m;j++){
  58. time[i][j]=inf;
  59. dp[i][j]=inf;
  60. }
  61. }
  62.  
  63. }
  64. int main(){
  65. int T;
  66. scanf("%d",&T);
  67. for(int ti=1;scanf("%d%d",&n,&m)==2&&ti<=T;ti++){
  68. for(int i=0;i<n;i++){
  69. scanf("%s",maz[i]);
  70. }
  71. init();
  72. int sx,sy;
  73. for(int i=0;i<n;i++){
  74. for(int j=0;j<m;j++){
  75. if(maz[i][j]=='F'){
  76. que.push(i*maxn+j);
  77. time[i][j]=0;
  78. }
  79. else if(maz[i][j]=='J'){
  80. sx=i;
  81. sy=j;
  82. }
  83. }
  84. }
  85. bfs();
  86. int ans=bfs2(sx,sy);
  87.  
  88. if(ans!=-1)printf("%d\n",ans);
  89. else puts("IMPOSSIBLE");
  90. }
  91. return 0;
  92. }

  

UVA 11624 Fire! bfs 难度:0的更多相关文章

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

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

  2. UVA 11624 Fire! (bfs)

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

  3. UVA 11624 Fire! BFS搜索

    题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...

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

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

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

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

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

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

  7. UVA - 11624 多点bfs [kuangbin带你飞]专题一

    题意:某人身陷火场,总有k个点着火,着火点可向四周扩散,问此人能否逃离. 思路:可能有多个着火点,以这些着火点作为起点进行bfs,得到整个火场的最短距离,然后又以人所在坐标作为起点进行bfs,得到该人 ...

  8. UVA 11624 - Fire! 图BFS

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

  9. UVa 11624 Fire!(BFS)

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

随机推荐

  1. DNS中A记录和CNAME记录的区别(转)

    A记录是域名到ip的映射,即为ip起别名:CNAME是域名别名到域名的映射,即为域名起别名. 还有一个常用的记录是MX记录,它是与邮件相关的,MX记录记录了发送电子邮件时域名对应的服务器地址. 原文: ...

  2. 使用Navicat连接Mysql报错:can not get hostname for your address

    以管理员的身份使用cmd命令运行netsh winsock reset即可!

  3. Mirror--镜像相关操作

    其他相关操作1. 关闭镜像--关闭镜像USE [master]GOALTER DATABASE Demo1 SET PARTNER OFFGO  2. 证服务器--移除见证服务器USE [master ...

  4. 百度天气接口api

    百度天气接口 以GET形式提交,返回JSON或XML URL:http://api.map.baidu.com/telematics/v3/weather?location={城市名}&out ...

  5. Underscore.js (1.7.0)-函数预览

    集合(Collections)(25) - each - map - reduce - reduceRight - find - filter - where - findWhere - reject ...

  6. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  7. Deep Learning(1)

    深度学习是机器学习研究中的一个新的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据,例如图像,声音和文本.深度学习是无监督学习的一种. 深度学习的概念源于人工神经网络的 ...

  8. web前端几个小知识点笔记

    1.css实现宽度是百分比的盒子为正方形 <div style="width:50%;padding-bottom:50%;height:0px;background:#ccc;&qu ...

  9. .net core 2.2 & Mongodb

    .net core 2.2 API项目中使用Mongodb 简单的CRUD封装 创建FoodPlan.Core 项目 创建IEntityBase.cs 接口约束 创建Single.cs 实体 IEnt ...

  10. Cooperation.GTST团队第四周项目总结

    项目进展 这周我们的主要学习内容是: 1.研究学习如何导入博客详情页. 2.继续研究如何使用博客园的相关接口,导入相关jar包实现页面整体效果: 在我们使用其它APP或者上网浏览论坛.网页等时,通常都 ...