算法指南白书

分别求一次人和火到达各个点的最短时间

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int INF = ;
  8. const int maxr = + ;
  9. const int maxc = + ;
  10. int R, C;
  11. char maze[maxr][maxc];
  12.  
  13. struct Cell {
  14. int r, c;
  15. Cell(int r, int c):r(r),c(c) {}
  16. };
  17.  
  18. const int dr[] = {-,,,};
  19. const int dc[] = {,,-,};
  20. int d[maxr][maxc][], vis[maxr][maxc][];//0表示人,1表示火走
  21.  
  22. queue<Cell> Q;
  23. void bfs(int kind) {
  24. while(!Q.empty()) {
  25. Cell cell = Q.front();
  26. Q.pop();
  27. int r = cell.r, c = cell.c;
  28. for(int dir = ; dir < ; dir++) {
  29. int nr = r + dr[dir], nc = c + dc[dir];
  30. if(nr >= && nr < R && nc >= && nc < C && maze[nr][nc] == '.' && !vis[nr][nc][kind]) {
  31. Q.push(Cell(nr, nc));
  32. vis[nr][nc][kind] = ;
  33. d[nr][nc][kind] = d[r][c][kind] + ;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. int ans;
  40. void check(int r, int c) {
  41. if(maze[r][c] != '.' || !vis[r][c][]) return; // 必须是Joe可达的边界格子
  42. if(!vis[r][c][] || d[r][c][] < d[r][c][]) ans = min(ans, d[r][c][] + ); // Joe必须先于火到达
  43. }
  44.  
  45. int main() {
  46. int T;
  47. scanf("%d", &T);
  48. while(T--) {
  49. scanf("%d%d", &R, &C);
  50. int jr, jc;
  51. vector<Cell> fires;
  52. for(int i = ; i < R; i++) {
  53. scanf("%s", maze[i]);
  54. for(int j = ; j < C; j++)
  55. if(maze[i][j] == 'J') {
  56. jr = i;
  57. jc = j;
  58. maze[i][j] = '.';
  59. } else if(maze[i][j] == 'F') {
  60. fires.push_back(Cell(i,j));
  61. maze[i][j] = '.';
  62. }
  63. }
  64. memset(vis, , sizeof(vis));
  65.  
  66. // Joe
  67. vis[jr][jc][] = ;
  68. d[jr][jc][] = ;
  69. Q.push(Cell(jr, jc));
  70. bfs();
  71.  
  72. // Fire
  73. for(int i = ; i < fires.size(); i++) {
  74. vis[fires[i].r][fires[i].c][] = ;
  75. d[fires[i].r][fires[i].c][] = ;
  76. Q.push(fires[i]);
  77. }
  78. bfs();
  79.  
  80. // 计算答案
  81. ans = INF;
  82. for(int i = ; i < R; i++) {
  83. check(i,);
  84. check(i,C-);
  85. }
  86. for(int i = ; i < C; i++) {
  87. check(,i);
  88. check(R-,i);
  89. }
  90. if(ans == INF) printf("IMPOSSIBLE\n");
  91. else printf("%d\n", ans);
  92. }
  93. return ;
  94. }

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

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

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

  2. UVA 11624 Fire! bfs 难度:0

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

  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 - Fire! 图BFS

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

  8. UVa 11624 Fire!(BFS)

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

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

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

随机推荐

  1. csqlite编译相关配置问题

    csqlite是非常好用的数据库,同时该数据库是开源的,基于一定原因可能需要编译自己需要的csqlite版本,那么下面介绍内容也会你就会感兴趣了. 这里要实现的目标是使用VS工具能够正确编译csqli ...

  2. find命令使用, -exec xargs

    find [path]   [expression] 例如:find  /home  -name  \*.o  -exec rm '{}' \; find: 实时精确,支持众多查找标准,遍历指定目录中 ...

  3. 网络ip

    国际规定:把所有的IP地址划分为 A,B,C,D,E A类地址:范围从0-127,0是保留的并且表示所有IP地址,而127也是保留的地址,并且是用于测试环回用的.因此 A类地址的范围其实是从1-126 ...

  4. H5 required 改变错误提示oninvalid、oninput、onforminput

    <input type="text" name="password" oninvalid="this.setCustomValidity('XX ...

  5. CentOS使用ufw的方法

    ufwはファイアウォールの管理ツールで.Ubuntuで標準的に使われています.ufw allow 80/tcp のような簡単なコマンドでポートを開け閉めできます. CentOS用のパッケージは用意され ...

  6. TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间

    使用函数: System.IOUtils.TDirectory.GetCreationTime//获取创建时间 System.IOUtils.TDirectory.SetCreationTime//设 ...

  7. Soy文件生成JS文件 - 一个使用Google soy模板的例子

    1.下载工具包,后解压. http://closure-templates.googlecode.com/files/closure-templates-for-javascript-latest.z ...

  8. logger.debug,logger.info,logger.warn,logger.error,logger.fatal的区别

    logger.debug,logger.info,logger.warn,logger.error,logger.fatal的区别 logger.debug,logger.info,logger.wa ...

  9. Android 获取手机联系人信息

    //获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Conten ...

  10. 【POJ3169 】Layout (认真的做差分约束)

    Layout   Description Like everyone else, cows like to stand close to their friends when queuing for ...