90pts WA on test8 求助

P1649 [USACO07OCT] Obstacle Course S

yinbe2 @ 2024-04-20 17:08:45

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int vis[105][105];
int ans=0x3f3f3f3f;
int n,tx,ty;
struct path
{
    int x,y,step,f;
};
queue<path>q;
int main()
{
    memset(vis,0x3f3f3f3f,sizeof(vis));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            char c;
            cin>>c;
            if(c=='x')vis[i][j]=-1;
            if(c=='A')q.push({i,j,0,3}); 
            if(c=='B')
            {
                tx=i;
                ty=j;
            }
        }
    }
    while(!q.empty())
    {
        path a=q.front();
        q.pop();    
        if(vis[a.x][a.y]==-1)continue;
        if(a.step>=ans)continue;
        if(a.x<=0||a.y<=0||a.x>n||a.y>n)continue;
        if(a.x==tx&&a.y==ty)
        {
            ans=min(ans,a.step);
            continue;
        }
        if(a.step>=vis[a.x][a.y])continue;
        vis[a.x][a.y]=a.step;
        if(a.f==3)
        {
            q.push({a.x+1,a.y,a.step,1});
            q.push({a.x-1,a.y,a.step,1});
            q.push({a.x,a.y+1,a.step,0});
            q.push({a.x,a.y-1,a.step,0});
            continue;
        }
        if(a.f==1)
        {
            q.push({a.x+1,a.y,a.step,1});
            q.push({a.x-1,a.y,a.step,1});
            q.push({a.x,a.y+1,a.step+1,0});
            q.push({a.x,a.y-1,a.step+1,0});
            continue;
        }
        if(a.f==0)
        {
            q.push({a.x+1,a.y,a.step+1,1});
            q.push({a.x-1,a.y,a.step+1,1});
            q.push({a.x,a.y+1,a.step,0});
            q.push({a.x,a.y-1,a.step,0});
            continue;
        }
    }
    if(ans==0x3f3f3f3f)printf("-1");
    else printf("%d",ans);
    return 0;
}

|