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;
}

原文地址:https://www.cnblogs.com/lokiii/p/9206424.html

时间: 2024-08-03 19:34:42

bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】的相关文章

1671: [Usaco2005 Dec]Knights of Ni 骑士

1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 254  Solved: 163[Submit][Status][Discuss] Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarde

【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木并带一棵给他们.当然,贝茜想早点离开这可怕的森林,于是她必须尽快完成骑士们给的任务,贝茜随身带着这片森林的地图,地图上的森林被放入了直角坐标系,并按x,y轴上的单位长度划分成了W×H(1≤W,H≤1000)块,贝茜在地图上查出了她自己

POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士

1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 281  Solved: 180[Submit][Status][Discuss] Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarde

[Usaco2005 Dec]Knights of Ni 骑士

Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Tim

BZOJ1671 [Usaco2005 Dec]Knights of Ni 骑士

打水题啊打水题...然后就被虐了... 看不懂题目,后来查hzwer的blog,发现时题目翻译错了...我去我就说我的语文怎么这么差. hzwer:"总之就是在地图上从2出发,走到3,途中要至少经过一个4." 原来如此,不就是两次bfs嘛...只是第一次bfs的时候要注意不能经过4. 然后开始bfs,在快要写死的时候...终于对了... 哈哈,status第六哦~~~不错不错 1 /****************************************************

BZOJ1671: [Usaco2005 Dec]Knights of Ni

1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 175  Solved: 107[Submit][Status][Discuss] Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded b

BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most

BZOJ 1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配

Description 约翰的N(1≤N≤100000)只奶牛中出现了K(1≤K≤25000)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的奶牛排好队进入牛棚,同时需要你的慧眼来识别坏蛋,为了区分,约翰给所有奶牛都发了号牌,上面写着一个1..S(1≤S≤25)之间的数字.虽然这不是一个完美的方法,但也能起一点作用.现在,约翰已经不记得坏蛋们的具体号码.但是凭他的记忆,他给出一个"模式串".原坏蛋的号码如果相同,模式串中他们的号码依然相

bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】

在洛谷上被卡了一个点开了O2才过= = bfs即可,为方便存储,把所有坐标+500 #include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=1005,dx[]={-1,1,0,0},dy[]={0,0,-1,1}; int n,sx,sy; bool a[N][N],v[N][N]; struct qwe { int x,y,b; qwe(int X=0,i