BFS10分求助!!

P1649 [USACO07OCT] Obstacle Course S

UntilR @ 2020-09-12 17:36:20

rt

大佬帮帮蒟蒻吧

谢谢谢谢

#include <bits/stdc++.h>
using namespace std;
#define pa pair<int,int>
priority_queue<pa,vector<pa>,greater<pa> > line;
struct dot
{
    int x,y,direction;
}a[1000001];
int n,dx,dy,top;
int xx[5]={0,0,1,0,-1};
int yy[5]={0,1,0,-1,0};
bool d[3][101][101];
int main()
{
    std::ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            string s;
            cin>>s;
            if(s[0]=='x')
                for(int k=1;k<=2;k++)
                    d[k][i][j]=1;
            if(s[0]=='A')
            {
                a[++top].x=i;
                a[top].y=j;
                pa q(0,top);
                line.push(q);
            }
            if(s[0]=='B')
                dx=i,dy=j;
        }
    while(!line.empty())
    {
        int q=line.top().second,e=line.top().first;
        line.pop();
        if(d[a[q].direction][a[q].x][a[q].y]==1)
            continue;
        cout<<a[q].x<<" "<<a[q].y<<" "<<a[q].direction<<endl;
        d[a[q].direction][a[q].x][a[q].y]=1;
        if(a[q].x==dx&&a[q].y==dy)
        {
            cout<<e;
            goto end;
        }
        for(int i=1;i<=4;i++)
            if(a[q].x+xx[i]>0&&a[q].x+xx[i]<=n&&a[q].y+yy[i]>0&&a[q].y+yy[i]<=n&&d[i%2+1][a[q].x+xx[i]][a[q].y+yy[i]]==0)
            {
                a[++top].x=a[q].x+xx[i];
                a[top].y=a[q].y+yy[i];
                a[top].direction=i%2+1;
                if((i%2+1)!=a[q].direction&&a[q].direction!=0)
                    e++;
                pa w(e,top);
                line.push(w);
            }
    }
    cout<<"-1";
    end:;
    return 0;
}

|