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.database.sqlite.SQLiteException: no such column: aaa (code 1): , while compiling: DELETE FROM users WHERE user_name=aaa解决办法
在写安卓登录注册时注销按钮闪退发现: 这是因为此处错误: 因为用户名为字符串,不是整型,数据库查询要引号,少了引号查询不了,导致闪退 解决后成功运行 正确用法: 下次谨记,细节决定成败呀!
- docker安装wnameless/oracle-xe-11g并运行(手写超详细)
前景:没事想玩下linux,想着以后可以部署下自己的web项目上去,然后我就想装个oracle来着...之前都不懂linux来着,只知道公司的项目都是部署在上面,然后从装系统到装完oracle用了近五 ...
- Nodejs 操作 Sql Server
Nodejs 操作 Sql Server Intro 最近项目需要爬取一些数据,数据有加密,前端的js又被混淆了,ajax请求被 hook 了,有些复杂,最后打算使用 puppeteer 来爬取数据. ...
- centos6.5上进行crontab操作
1.service crond start 2. vi /home/cron.ini */ * * * * /home/monitor.sh 3.crontab /home/cron.ini OK
- 操作DataTable数据,修改某列的值
DataTable table : DataRow row=table.Rows[i];//DataTable的第i行 row.BeginEdit();//开始编辑行 row["column ...
- Java开发学习心得(二):Mybatis和Url路由
目录 Java开发学习心得(二):Mybatis和Url路由 1.3 Mybatis 2 URL路由 2.1 @RequestMapping 2.2 @PathVariable 2.3 不同的请求类型 ...
- css_属性
老师的博客:https://www.cnblogs.com/liwenzhou/p/7999532.htm css的属性 整体属性的:作用于全局 width:表示宽 height:表示长 color: ...
- C#基础知识之属性
其实属性大家经常用,可以说是非常熟悉了,这里就记录一下我那天突然对属性产生的疑惑.为什么需要使用属性?属性的好处是什么? 一.什么是属性? 属性(Property) 是类(class).结构(stru ...
- Ambari与Kerberos 集成
Kerberos 介绍 Kerberos 是一个网络认证的框架协议,其设计的初衷便是通过密钥系统为 Client 和 Server 应用程序之间提供强大的认证服务.在使用 Kerberos 认证的集群 ...
- Installing Supervisor and Superlance on CentOS
Installing Supervisor1 and Superlance2 on CentOS/RHEL/Fedora can be a little tricky, as the versions ...