P1699 [USACO19OPEN] Bucket Brigade B的题解

· · 题解

题目理解:

  1. 模拟队列,对还没学 STL 的初学者比较友好。模拟队列,也就是将数组模拟成队列。但需要标记头和尾的位置,还不能做到实质意义上的删除队头,其实空间没有减小,所以比较占空间,有些题目可能数组开不下,可是会更好理解。就来看看我的代码吧。
#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x;
    int y;
    int step;
};
string s[11];
node val[10*10+1];
int mx[]={0,0,1,-1};
int my[]={1,-1,0,0};
bool flag[11][11];
int n,m;
void bfs(int sx,int sy,int tx,int ty)
{
    int head=0;
    int tail=0;
    node p;
    p.x=sx;
    p.y=sy;
    p.step=0;
    flag[sx][sy]=true;
    val[tail++]=p; 
    while(head<tail)
    {
        node l=val[head];
        head++;
        for(int i=0;i<4;i++)
        {
            int bx=l.x+mx[i];
            int by=l.y+my[i];
            if(bx<0 || bx>=10 || by<0 || by>=10)
            {
                continue;
            }
            if(s[bx][by]=='R' || flag[bx][by]==true)
            {
                continue;
            }
            node q;
            flag[bx][by]=true;
            q.x=bx;
            q.y=by;
            q.step=l.step+1;
            val[tail++]=q;
            if(bx==tx && by==ty)
            {
                cout<<q.step-1;
                return;
            }
        }
    }
    cout<<-1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int sx,sy,tx,ty;
    for(int i=0;i<10;i++)
    {
        cin>>s[i];
        for(int j=0;j<10;j++)
        {
            if(s[i][j]=='L')
            {
                sx=i;
                sy=j;
            }
            if(s[i][j]=='B')
            {
                tx=i;
                ty=j;
            }
        }
    }

    bfs(sx,sy,tx,ty);
}

最快速度

最优所有测试点用时 34 毫秒。

最小内存

最优最大内存 680.00 KB。

  1. 直接使用队列。这样会方便很多,而且相对而言对空间的需求不是那么的大。但需要一定的 STL 基础。就来看看我的代码吧。
#include<bits/stdc++.h>
using namespace std;
string s[11];
bool flag[11][11];
int x_wei[]={0,0,-1,1};
int y_wei[]={1,-1,0,0};
int mn=110;
struct node
{
    short x;
    short y;
    short step;
}a;
void bfs(int x1,int y1,int x2,int y2)
{
    queue<node> q;
    node a;
    a.x=x1;
    a.y=y1;
    a.step=0;
    q.push(a);
    while(!q.empty())
    {
        node f=q.front();
        q.pop();
        if(f.x==x2 && f.y==y2)
        {
            mn=min(mn,f.step-1);
        }
        for(int l=0;l<4;l++)
        {
            int dx=f.x+x_wei[l];
            int dy=f.y+y_wei[l];
            if(dx>=0 && dx<10 && dy>=0 && dy<10)
            {
                if(!flag[dx][dy])
                {
                    if(s[dx][dy]!='R')
                    {
                        flag[dx][dy]=true;
                        node k;
                        k.x=dx;
                        k.y=dy;
                        k.step=f.step+1;
                        q.push(k);
                    }
                }
            }
        }
    }
    cout<<mn;
    return;
}
int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0),cout.tie(0);
    int x1=0,y1=0,x2=0,y2=0;
    for(int i=0;i<10;i++)
    {
        cin>>s[i];
        for(int j=0;j<10;j++)
        {
            if(s[i][j]=='L')
            {
                x1=i;
                y1=j;
            }
            if(s[i][j]=='B')
            {
                x2=i;
                y2=j;
            }
        }
    }
    flag[x1][y1]=true;
    bfs(x1,y1,x2,y2);
} 

最快速度

最优所有测试点用时 31 毫秒。

最小内存

最优最大内存 632.00 KB。

小结: