bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y])

  1. /*
  2. 0:贝茜可以通过的空地
  3. 1:由于各种原因而不可通行的区域
  4. 2:贝茜现在所在的位置
  5. 3:骑士们的位置
  6. 4:长着贝茜需要的灌木的土地
  7. */
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<queue>
  11. #include<cstring>
  12. using namespace std;
  13. const int N=1005,inf=1e9,dx[]={-1,1,0,0},dy[]={0,0,-1,1};
  14. int n,m,a[N][N],d1[N][N],d2[N][N],ans=inf;
  15. bool v[N][N];
  16. struct qwe
  17. {
  18. int x,y,p;
  19. qwe(int X=0,int Y=0,int P=0)
  20. {
  21. x=X,y=Y,p=P;
  22. }
  23. }s,t;
  24. int read()
  25. {
  26. int r=0,f=1;
  27. char p=getchar();
  28. while(p>'9'||p<'0')
  29. {
  30. if(p=='-')
  31. f=-1;
  32. p=getchar();
  33. }
  34. while(p>='0'&&p<='9')
  35. {
  36. r=r*10+p-48;
  37. p=getchar();
  38. }
  39. return r*f;
  40. }
  41. bool ok(int x,int y)
  42. {
  43. return x>=1&&x<=n&&y>=1&&y<=m&&!v[x][y]&&a[x][y]!=1;
  44. }
  45. void bfs(qwe s)
  46. {
  47. queue<qwe>q;
  48. memset(v,0,sizeof(v));
  49. for(int i=1;i<=n;i++)
  50. for(int j=1;j<=m;j++)
  51. d2[i][j]=inf;
  52. v[s.x][s.y]=1;
  53. d2[s.x][s.y]=0;
  54. q.push(s);
  55. while(!q.empty())
  56. {
  57. qwe u=q.front();
  58. q.pop();
  59. for(int i=0;i<4;i++)
  60. if(ok(u.x+dx[i],u.y+dy[i]))
  61. {
  62. v[u.x+dx[i]][u.y+dy[i]]=1;
  63. d2[u.x+dx[i]][u.y+dy[i]]=u.p+1;
  64. q.push(qwe(u.x+dx[i],u.y+dy[i],u.p+1));
  65. }
  66. }
  67. }
  68. int main()
  69. {
  70. m=read(),n=read();
  71. for(int i=1;i<=n;i++)
  72. for(int j=1;j<=m;j++)
  73. {
  74. a[i][j]=read();
  75. if(a[i][j]==2)
  76. s=qwe(i,j,0);
  77. if(a[i][j]==3)
  78. t=qwe(i,j,0),a[i][j]=1;
  79. }
  80. bfs(s);
  81. for(int i=1;i<=n;i++)
  82. for(int j=1;j<=m;j++)
  83. d1[i][j]=d2[i][j];
  84. a[t.x][t.y]=3;
  85. bfs(t);
  86. for(int i=1;i<=n;i++)
  87. for(int j=1;j<=m;j++)
  88. if(a[i][j]==4)
  89. ans=min(ans,d1[i][j]+d2[i][j]);
  90. printf("%d\n",ans);
  91. return 0;
  92. }

bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】的更多相关文章

  1. BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...

  2. 1671: [Usaco2005 Dec]Knights of Ni 骑士

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 254  Solved: 163 ...

  3. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  4. 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...

  5. POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 281  Solved: 180 ...

  6. bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士

    Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...

  7. BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS

    Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...

  8. [Usaco2005 Dec]Knights of Ni 骑士

    Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...

  9. BZOJ1671: [Usaco2005 Dec]Knights of Ni

    1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 175  Solved: 107[Su ...

随机推荐

  1. java中装箱与拆箱

    转载自:https://www.cnblogs.com/dolphin0520/p/3780005.html 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若 ...

  2. MySQL数据库:SQL语句基础、库操作、表操作、数据类型、约束条件、表之间的关系

    数据库相关概念: 1. 数据库服务器:运行数据库管理软件的计算机 2. 数据库管理软件:MySQL.Oracle.db2.slqserver 3. 库:文件夹,用来组织文件/表 4. 表:文件(类似于 ...

  3. 【HDOJ6146】Pokémon GO(DP,计数)

    题意:一个2*n的矩阵,从任意一格出发,不重复且不遗漏地走遍所有格子,问方案数 mo 10^9+7 n<=10000 思路:因为OEIS搜出来的两个数列都是错误的,所以考虑DP 设B[i]为2* ...

  4. 使用MediaPlayer播放、暂停、停止音乐

    package com.pingyijinren.test; import android.media.MediaPlayer; import android.os.Environment; impo ...

  5. 安装最新版本的zabbix

    1. 先安装php5.4 最新版本: yum安装php5.4或5.5 https://blog.csdn.net/MarkBoo/article/details/49424183 2. 然后参照官网或 ...

  6. openstack setup demo Image service

    Image service (glance)是openstack中管理vm image的service.本文包含以下内容: overview install overview glance包含以下部分 ...

  7. ubuntu12.04+cuda6.0+opencv2.4.9

    更新了cuda之后,opencv的gpu模块又要重新编译了,这个地方有一个疑问,我对cuda6.0装了两次,第一次装好之后,没有配一个bumblebee,重装了cuda6.0之后,发现原来编译的ope ...

  8. mysql的时间戳说白了就俩问题,自动更新问题和不自动更新问题

    mysql的时间戳timestamp说白了就俩问题,自动更新问题和不自动更新问题

  9. Map根据value排序ASC DESC

    原文:http://blog.csdn.net/k21325/article/details/53259180 需求有点刁钻,写关键词组合匹配标题的时候,遇到关键词像这样 XXX XXX 1222 X ...

  10. Redux 中文文档

    http://cn.redux.js.org/docs/introduction/Ecosystem.html