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 ...
随机推荐
- 使用C++调用pytorch模型(Linux)
前言 模型转换思路通常为: Pytorch -> ONNX -> TensorRT Pytorch -> ONNX -> TVM Pytorch -> 转换工具 -> ...
- python模糊匹配之fuzzywuzzy
fuzzywyzzy 是python下一个模糊匹配的模块.首先要安装fuzzywuzzy 示例: from fuzzywuzzy import fuzz from fuzzywuzzy import ...
- vs2003 刷新项目失败。无法从服务器中检索文件夹信息
环境: 操作系统:windows server 2003 开发工具:Visual stuadio 2003 FrameWork: 1.1 打开web项目的时候报错 提示 项目刷新失败,无法从服务器 ...
- Webdriver概述(selenium对应浏览器版本)
Webdriver (Selenium2)是一种用于Web应用程序的自动测试工具,它提供了一套友好的API,与Selenium 1(Selenium-RC)相比,Webdriver 的API更容易理解 ...
- BNUOJ 1268 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 11496 ...
- <转> 二分图多重匹配问题
在二分图最大匹配中,每个点(不管是X方点还是Y方点)最多只能和一条匹配边相关联,然而,我们经常遇到这种问题,即二分图匹配中一个点可以和多条匹配边相关联,但有上限,或者说,Li表示点i最多可以和多少条匹 ...
- SPOJ 3267 D-query (可持久化线段树,区间重复元素个数)
D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair ...
- 解方程(codevs 3732)
题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...
- 使用Post方法模拟登陆爬取网页(转)
使用Post方法模拟登陆爬取网页 最近弄爬虫,遇到的一个问题就是如何使用post方法模拟登陆爬取网页.下面是极简版的代码: import java.io.BufferedReader; impor ...
- 设计模式之外观模式(Facade)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...