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:贝茜现在所在的位置
3:骑士们的位置
4:长着贝茜需要的灌木的土地
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=1005,inf=1e9,dx[]={-1,1,0,0},dy[]={0,0,-1,1};
int n,m,a[N][N],d1[N][N],d2[N][N],ans=inf;
bool v[N][N];
struct qwe
{
int x,y,p;
qwe(int X=0,int Y=0,int P=0)
{
x=X,y=Y,p=P;
}
}s,t;
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool ok(int x,int y)
{
return x>=1&&x<=n&&y>=1&&y<=m&&!v[x][y]&&a[x][y]!=1;
}
void bfs(qwe s)
{
queue<qwe>q;
memset(v,0,sizeof(v));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
d2[i][j]=inf;
v[s.x][s.y]=1;
d2[s.x][s.y]=0;
q.push(s);
while(!q.empty())
{
qwe u=q.front();
q.pop();
for(int i=0;i<4;i++)
if(ok(u.x+dx[i],u.y+dy[i]))
{
v[u.x+dx[i]][u.y+dy[i]]=1;
d2[u.x+dx[i]][u.y+dy[i]]=u.p+1;
q.push(qwe(u.x+dx[i],u.y+dy[i],u.p+1));
}
}
}
int main()
{
m=read(),n=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
a[i][j]=read();
if(a[i][j]==2)
s=qwe(i,j,0);
if(a[i][j]==3)
t=qwe(i,j,0),a[i][j]=1;
}
bfs(s);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
d1[i][j]=d2[i][j];
a[t.x][t.y]=3;
bfs(t);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]==4)
ans=min(ans,d1[i][j]+d2[i][j]);
printf("%d\n",ans);
return 0;
}
bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】的更多相关文章
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- 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 ...
随机推荐
- Leetcode 188.买卖股票的最佳时机IV
买卖股票的最佳时机IV 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 k 笔交易. 注意: 你不能同时参与多笔交易(你必 ...
- [luoguP1472] 奶牛家谱 Cow Pedigrees(DP)
传送门 一个深度为i的树可以由一个根节点外加两个深度为i-1的树组成,这就决定了DP该怎么写. 然而我真的没有想到. f[i][j]表示深度为i节点数为j的个数 sum[i][j]表示深度小于等于i节 ...
- noip模拟赛 少女
分析:每个连通块都是独立的,对一个连通块进行分析.如果边数>点数,显然是不可能的,因为每条边要分配给一个点,至少有一个点分配了两次以上.如果边数=点数,就形成了环,有两种方案,顺时针一个环,逆时 ...
- boot简介
目录 1:bootloader介绍2:如何启动一个机器3:工具 Bootloader介绍 MTK的bootloader 主要分为Pre-loader, LK. Pre-loader: 初始化PLL,U ...
- 在workbench中导入.sql文件!(导入数据库文件)
第一步,登陆mysql workbench 第二步,打开自己的数据 ,此处默认(root) 打开数据库后页面 : 第三步,新建一个schema ,随便给个名字,这里起名为test : 可以看到test ...
- JSTL-XML标签库
主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 一.<x:out> <x:out>标签显示XPath表达式的结果, ...
- php-7.1编译记录
编译php-7.1.28步骤 检查环境 ./configure \ --prefix=/u01/server/php-7.1.28 \ --enable-fpm \ --with-fpm-user=d ...
- 深入理解windows系统内的GMT和时区
http://www.itshanghai.net/technology/wdzl_windowsxp/ UTC(Universal Time Coordinated)是通用协调时,这两者几乎是一 ...
- the apple code
i know you will forget but 9 you will
- 移动端 js 实现图片上传 预览
方法一: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&q ...