题目:

  https://www.lydsy.com/JudgeOnline/problem.php?id=1671

题解:
  按题意分别从贝茜和骑士bfs然后meet_in_middle。。

  把一个逗号打成分号调了20分钟。。。

代码:

 #include<bits/stdc++.h>

 using namespace std;

 const int mx[]={,-,,,},my[]={,,,-,};
const int maxn=;
int n,m,g[maxn][maxn],vis[maxn][maxn],f[maxn][maxn],sum,cnt,sx,sy,bx,by,flag,ans=0x3f3f3f3f;
struct node{int x,y,num;}; int main(){
scanf("%d%d",&m,&n);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%d",&g[i][j]);
if(g[i][j]==) sx=i,sy=j;
else if(g[i][j]==) bx=i,by=j;
else if(g[i][j]==) sum++;
}
queue<node> q; cnt=sum;
q.push((node){sx,sy,}),vis[sx][sy]=;
while(!q.empty()){
node now=q.front(); q.pop();
for(int i=;i<=;i++){
int xx=now.x+mx[i],yy=now.y+my[i];
if(xx>&&xx<=n&&yy>&&yy<=m&&!vis[xx][yy]&&g[xx][yy]!=){
if(g[xx][yy]==&&f[xx][yy]==) f[xx][yy]=now.num+,cnt--;
if(!cnt) goto end;
q.push((node){xx,yy,now.num+}); vis[xx][yy]=;
}
}
}
end: memset(vis,,sizeof(vis));
while(!q.empty()) q.pop(); cnt=sum;
q.push((node){bx,by,}),vis[bx][by]=;
while(!q.empty()){
node now=q.front(); q.pop();
for(int i=;i<=;i++){
int xx=now.x+mx[i],yy=now.y+my[i];
if(xx>&&xx<=n&&yy>&&yy<=m&&!vis[xx][yy]&&g[xx][yy]!=){
if(g[xx][yy]==) ans=min(now.num++f[xx][yy],ans),cnt--;
if(!cnt) goto print;
q.push((node){xx,yy,now.num+}); vis[xx][yy]=;
}
}
}
print: printf("%d",ans);
return ;
}

BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)的更多相关文章

  1. bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】

    bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...

  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. PJSUA2开发文档--第九章 PJSUA2应用程序示例

    9. PJSUA2示例应用程序 9.1 示例应用程序 9.1.1 C++ pjsip-apps/src/samples/pjsua2_demo.cpp 是一个非常简单可用的C++示例应用程序. /* ...

  2. ASP.NET Zero--前期要求

    前期要求 需要以下工具才能使用ASP.NET Zero Core解决方案: Visual Studio 2017 + Visual Studio扩展: Bundler&Minifier Web ...

  3. LeetCode算法题-N-ary Tree Preorder Traversal(Java实现)

    这是悦乐书的第268次更新,第282篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第135题(顺位题号是589).给定一个n-ary树,返回其节点值的前序遍历.例如,给定 ...

  4. Spark RDD持久化、广播变量和累加器

    Spark RDD持久化 RDD持久化工作原理 Spark非常重要的一个功能特性就是可以将RDD持久化在内存中.当对RDD执行持久化操作时,每个节点都会将自己操作的RDD的partition持久化到内 ...

  5. vue2.0阻止事件冒泡

    <!--picker弹窗--><transition name="fade"> <div class="picker_wrap" ...

  6. 为什么 npm 要为每个项目单独安装一遍 node_modules?

    nodejs中package.json中的依赖必须每个项目都有自己的node_modules文件夹,而无法在多个项目之间共用一套node_modules(像Java中的Maven那样). 依赖管理是每 ...

  7. RPC是什么?

    初学微服务,一点会问RPC是什么,通常网上的资料会说,是一种协议,然后说得很复杂,一堆概念,拜托,我只是想知道RPC是什么,而不是  怎么实现怎么做. RPC就是想实现函数调用模式的网络化,A服务(微 ...

  8. debian8下acme nginx 部署记录

    1.更新源 apt update 2.安装curl git apt install curl git -y 3.克隆acme仓库 curl https://get.acme.sh | sh git c ...

  9. Jenkins + Ansible + Gitlab之ansible篇

    Ansible介绍 什么是Ansible? Ansible是一个开源部署工具 开发语言:Python 特点:SSH协议通信,全平台,无需要编译,模块化部署管理 作用:推送Playbook进行远程节点快 ...

  10. 监控glusterfs

    监控集群状态 [4ajr@elk1 scripts]$ cat glusterfs_peer_status.sh #!/bin/bash peer_status=`sudo gluster peer ...