2AC2wa2MLE

P1649 [USACO07OCT] Obstacle Course S

laybare_mushroom @ 2024-09-22 16:48:13

#include<bits/stdc++.h>
using namespace std;
int n,tr[105][105],xxx,yyy;
struct P{
    int x,y,fang,step;
};
P u;
int sum=0;
queue<P>q;
char a[105][105];
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
            if(a[i][j]=='A'){
                tr[i][j]=3;
                xxx=i;
                yyy=j;
                //continue;
            }
            if(a[i][j]=='B'){
                tr[i][j]=5;
            //  continue;
            }
            if(a[i][j]=='x'){
                tr[i][j]=1;
                //continue;
            }
            if(a[i][j]=='.'){
                tr[i][j]=-1;
                //continue;
            }
            //tr[i][j]=-1;
        }
    }
    u.x=xxx;
    u.y=yyy;
    u.fang=0;
    u.step=0;
    q.push(u);
    P o;
    while(!q.empty()){
        u=q.front();
        //cout<<u.x<<" "<<u.y<<" "<<u.fang <<" "<<u.step<<endl;
        q.pop();
        if(tr[u.x+1][u.y]!=1&&tr[u.x+1][u.y]!=0){
    //      cout<<"asd1";
        o=u;
        o.x=u.x+1;
            if(o.fang!=1&&o.fang!=0&&o.fang!=3){
                o.step++;

            }
            o.fang=1;
            q.push(o);
            if(tr[o.x][o.y]==5){
        //  cout<<"asd1";
                cout<<o.step;
                return 0;
            }

            for(int j=2;;j++){

                if(tr[o.x+j][o.y]==5){
                //cout<<"asd1";     //
                cout<<o.step;
                return 0;
            }
                if(tr[o.x+j][o.y]!=-1||tr[o.x][o.y+1]==0){

                    o.x=o.x+j-1;
                o.y=o.y;
                o.fang=1;
                o.step=o.step;
                q.push(o);
                break;
                }

            }

    }
        if(tr[u.x-1][u.y]!=1&&tr[u.x-1][u.y]!=0){
            //cout<<"asd2";
        o=u;
        o.x=u.x-1;
            if(u.fang!=1&&u.fang!=0&&u.fang!=3){
                o.step++;
            //cout<<"asd";
            }
            //q.push(o);
            if(tr[o.x][o.y]==5){
                cout<<o.step;
                return 0;
            }
                o.fang=3;
                q.push(o);
            for(int j=2;;j++){
                if(tr[o.x-j][o.y]==5){
                cout<<o.step;
                return 0;
            }
                if(tr[o.x-j][o.y]!=-1||tr[o.x][o.y+1]==0){
                    o.x=o.x-j+1;
                o.y=o.y;
                o.fang=3;
                o.step=o.step;
                q.push(o);
                break;
                }

            }

        }
        if(tr[u.x][u.y+1]!=1&&tr[u.x][u.y+1]!=0){
            //cout<<"asd3";
        o=u;
        o.y=u.y+1;
            if(u.fang!=2&&u.fang!=0&&u.fang!=4){
                o.step++;
            //  cout<<"asd";
            }
            if(tr[o.x][o.y]==5){
                cout<<o.step;
                return 0;
            }
            o.fang=2;
            q.push(o);
            for(int j=2;;j++){
                            if(tr[o.x][o.y+j]==5){
                cout<<o.step;
                return 0;
            }
                if(tr[o.x][o.y+j]!=-1||tr[o.x][o.y+1]==0){
                    o.x=o.x;
                o.y=o.y+j-1;
                o.fang=2;
                o.step=o.step;
                q.push(o);
                break;
                }
            }

            }
        if(tr[u.x][u.y-1]!=1&&tr[u.x][u.y-1]!=0){
        //  cout<<"asd";
        o=u;
        o.y=u.y-1;
            if(o.fang!=2&&o.fang!=0&&o.fang!=4){
            o.step++;
            //  cout<<"asd";
            }
            if(tr[o.x][o.y]==5){
                cout<<o.step;

                return 0;
            }
            o.fang=4;
            q.push(o);
            for(int j=2;;j++){
            if(tr[o.x][o.y-j]==5){
                cout<<o.step;//cout<<o.x<<" "<<o.y-j<<" "<<o.fang <<" "<<o.step<<endl;
                return 0;
            }
                if(tr[o.x][o.y-j]!=-1||tr[o.x][o.y+1]==0){
                o.x=o.x;
                o.y=o.y-j+1;
                o.fang=4;
                o.step=o.step;  
                q.push(o);
                break;
                }
            }
            //  q.push(o);
            }
    }
    cout<<"-1";
    return 0;
} 

|