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 ...
随机推荐
- Python接口测试之报告(十五)
在本文章中,主要使用jenkins和编写的自动化测试代码,来生成漂亮的测试报告,关于什么是CI这些 我就不详细的介绍了,这里我们主要是实战为主. 首先搭建java的环境,这个这里不做介绍.搭建好jav ...
- HASH的应用(负数下标用偏移量解决)
Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个处于区间[-500000,500000]的整数. Output 对每组测试数据按从大到小的 ...
- [luoguP2709] 小B的询问(莫队)
传送门 个数 1 2 3 4 5 答案 1 4 9 16 25 做差 1 3 5 7 9 显然增加一个数只需要增加 ton[a[x]] << 1 | 1 即可 减去一个数也减去这个 注意 ...
- 文件权限设置与http,php的关系
在web服务器上的文件要使用什么权限比较好呢.我开始的时候直接都是777,后台安全部门的同事,通过漏洞把我管理的服务器给搞了.报告到我这里,我才意识到权限的设置不能马虎.环境采用nginx+php,一 ...
- [NOIP2007] 提高组 洛谷P1099 树网的核
题目描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各边长度的集合,并 ...
- [bzoj2287][poj Challenge]消失之物_背包dp_容斥原理
消失之物 bzoj-2287 Poj Challenge 题目大意:给定$n$个物品,第$i$个物品的权值为$W_i$.记$Count(x,i)$为第$i$个物品不允许使用的情况下拿到重量为$x$的方 ...
- Spring Cloud(5):Hystrix的使用
熔断:类似生活中的保险丝,电流过大就会熔断 降级:类似生活中的旅行,行李箱只有那么大,所以要抛弃一些非必需的物品 熔断降级应用: 某宝双十一商品下单,用户量巨大,于是考虑抛弃相关商品推荐等模块,确保该 ...
- JSP操作
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/actions.html: JSP操作(Action)使用XML语法结构来控制Servlet引擎的行为.可 ...
- Sqlserver数据库发送邮件
目录 1. Sqlserver数据库发送邮件 1.1. 概念了解 1.2. 配置 1.3. 测试发送邮件 1.3.1. 代码测试 1.3.2. 工具测试 1.4. 查看邮件日志 1. Sqlserve ...
- openstack setup demo Compute service
本文包含以下部分 Compute service overview Install and configure controller node Prerequisites Install and co ...