BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目:
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)的更多相关文章
- 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:由于各种原因而不可通 ...
- 1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 254 Solved: 163 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 281 Solved: 180 ...
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- 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 ...
- [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
随机推荐
- Android为TV端助力context转换类型
- Tips on GORM, Avoid Error about "duplicate column name: id"
The GORM is an super easy ORM solution for Go language. But many people would get the error about du ...
- Java新知识系列 七
抽象类和接口的区别和特点 java的JDK中包含的五个工具 编译型语言和解释型语言 Java和C++的区别` 常见的ASCII的值 Forward和Redirect之间的对比 Web Service ...
- ASP.NET Core 入门教程 3、ASP.NET Core MVC路由入门
一.前言 1.本文主要内容 ASP.NET Core MVC路由工作原理概述 ASP.NET Core MVC带路径参数的路由示例 ASP.NET Core MVC固定前/后缀的路由示例 ASP.NE ...
- SQL Server Mirror 断开后,删除副本上镜像数据库
一般而言,SQL Server 在数据库级别进行数据同步的方式主要有三种 1.日志传送:2.Mirror(镜像):3. AlwaysOn.复制订阅技术理解为表级别的同步,不归结为数据库级别的同步. 在 ...
- Xamarin移动开发的优点和缺点
在考虑iOS或Android应用程序开发时,我们大多数人会首先考虑Objective-C vs Swift和Java.作为本地技术堆栈,当涉及到iOS和Android应用程序开发时,它们自然是最常用的 ...
- Java https ssl证书导入删除
下载并命名 例如命名github.cer 放进jre的lib\security下 keytool -delete [OPTION]... 选项: -alias <alias> 要处理的条目 ...
- 浅谈TCP IP协议栈(四)IP协议解析
通过之前的网络层基础知识,IP地址以及路由器的简介,大家应该对于TCP/IP有一个大致的了解,在脑海里应该对于网络的几个基础概念有个大概的了解,简单点说整个协议栈就是在做一件事,规定网络报文(网络传输 ...
- 聚类——GMM
聚类——认识GMM算法 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.GMM概述 二.GMM算法步骤 三.具体推导参考文献 1. 李航. 统计学习 ...
- RabbitMQ基本示例,轮询机制,no_ack作用
一.RabbitMQ简介: ''' RabbitMQ就是消息队列 之前不是学了Queue了吗,都是队列还学RabbitMQ干嘛? 干的事情是一样的 Python的Queue有两个, 一个线程Queue ...