先对火BFS一次,求出每个点的最小着火时间。

再对人BFS一次,求出走到边界的最少时间。

  1. #include <iostream>
  2. #include <queue>
  3. #include <cstring>
  4. using namespace std;
  5. int n,m,bz[1005][1005],qihuo[1005][1005];
  6. char map[1005][1005];
  7. int h[4][2]={-1,0,1,0,0,-1,0,1};
  8. struct point
  9. {
  10. int x,y,step;
  11. };
  12. queue<point> q;
  13. void bfs_huo()
  14. {
  15. int i,j,x,y;
  16. point t,tt;
  17. while(!q.empty())
  18. {
  19. t=q.front(); q.pop();
  20. for(i=0;i<4;i++)
  21. {
  22. x=t.x+h[i][0]; y=t.y+h[i][1];
  23. if(x>=0&&x<n&&y>=0&&y<m&&(map[x][y]=='.'||map[x][y]=='J')&&!bz[x][y])
  24. {
  25. bz[x][y]=1;
  26. tt.x=x; tt.y=y; tt.step=t.step+1; qihuo[x][y]=tt.step;
  27. q.push(tt);
  28. }
  29. }
  30. }
  31. }
  32. int bfs_men(point s)
  33. {
  34. int i,j,x,y;
  35. point t,tt;
  36. while(!q.empty()) q.pop();
  37. q.push(s); bz[s.x][s.y]=1;
  38. while(!q.empty())
  39. {
  40. t=q.front(); q.pop();
  41. if(t.x<=0||t.x==n-1||t.y<=0||t.y==m-1) return t.step; //??磬?0
  42. for(i=0;i<4;i++)
  43. {
  44. x=t.x+h[i][0]; y=t.y+h[i][1];
  45. if(x>=0&&x<n&&y>=0&&y<m&&(map[x][y]=='.'||map[x][y]=='J')&&!bz[x][y])
  46. {
  47. if(qihuo[x][y]<=t.step+1) continue;
  48. bz[x][y]=1;
  49. tt.x=x; tt.y=y; tt.step=t.step+1;
  50. q.push(tt);
  51. }
  52. }
  53. }
  54. return -1;
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. int nn,i,j;
  59. point s,t;
  60. cin>>nn;
  61. while(nn--)
  62. {
  63. cin>>n>>m;
  64. while(!q.empty()) q.pop();
  65. memset(bz,0,sizeof(bz));
  66. for(i=0;i<n;i++)
  67. for(j=0;j<m;j++)
  68. {
  69. cin>>map[i][j]; qihuo[i][j]=1000000000; //???
  70. if(map[i][j]=='J') {s.x=i; s.y=j; s.step=0;}
  71. if(map[i][j]=='F') {t.x=i; t.y=j; t.step=0; q.push(t); bz[i][j]=1; qihuo[i][j]=0;}
  72. }
  73. bfs_huo();
  74. memset(bz,0,sizeof(bz));
  75. int ans=bfs_men(s);
  76. if(ans>=0) cout<<ans+1<<endl;
  77. else cout<<"IMPOSSIBLE"<<endl;
  78. }
  79. return 0;
  80. }

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

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

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

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

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

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

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

  4. UVA - 11624 Fire! 【BFS】

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

  5. UVA 11624 - Fire! 图BFS

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

  6. UVa 11624 Fire!(BFS)

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

  7. UVA 11624 Fire! (bfs)

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

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

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

  9. UVA 11624 Fire! bfs 难度:0

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

随机推荐

  1. Windows Server 2008中关闭事件跟踪程序的方法

    Windows Server 2008跟Windows Server 2003一样,在关机的时候会弹出一个“关闭事件跟踪程序”窗口,当然微软这么做是处于安全的考虑啦,但是如果我们只是个人用用的话,那就 ...

  2. CentOS 6使用iostat

    iostat/mpstat/sar等命令属于sysstat程序包,所以使用yum安装sysstat即可. yum install sysstat

  3. Python获取web页面信息

    import sys, urllib2 # req = urllib2.Request(sys.argv[1]) req = urllib2.Request('http://www.sina.com. ...

  4. Android 5.0 新特性

    Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ...

  5. web前端:html

    一.理解表单的作用 1.web 应用程序不仅仅是给用户显示数据,还应该给用户提供一个可以输入数据的图形用户界面.表单的主要作用在于在网页上提供一个图形用户界面,已采集和提交用户输入的数据. 2.htm ...

  6. javascript闭包分析

    闭包是什么?闭包是Closure,简而言之,闭包就是: 闭包就是函数的局部变量集合,只是这些局部变量在函数返回后会继续存在. 闭包就是就是函数的“堆栈”在函数返回后并不释放,我们也可以理解为这些函数堆 ...

  7. Using load balance for thrift servers

    Software load balance .Nginx(http://nginx.org) 1.Install nginx download source code from http://ngin ...

  8. 尽量不要用select into 复制表

    select into 复制表会带来灾难后果,因为只是复制了一个外壳,就像克隆人,有躯体没意识,像原表的主键 外键 约束 触发器 索引都不会被复制过来, 创建一个表:CREATE TABLE [dbo ...

  9. cocos2dx入门分析 hello world

    打开新建的"findmistress"项目,可以看到项目文件是由多个代码文件及文件夹组成的,其中 Hello World 的代码文件直接存放于该项目文件夹中.下面我们来详细介绍一下 ...

  10. [LeetCode OJ] Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...