求大神看一眼。。。第七个点死活不对啊!!

P2385 [USACO07FEB] Bronze Lilypad Pond B

lunatic @ 2017-06-20 07:49:21

#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
queue <short> x,y;
int n,m,a[405][405];
int ans1,ans2;
int main(){
    ios::sync_with_stdio(false);
    int m1,m2,bx,by;
    cin>>n>>m>>m1>>m2; 
    for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
                if(a[i][j]==4)
                    ans1=i,ans2=j;
                if(a[i][j]==3)
                    bx=i,by=j;
        }
    }
    x.push(bx);
    y.push(by);
    a[bx][by]=0;
    const int zh[8][2]={
        {m1,m2},
        {m1,-m2},
        {-m1,m2},
        {-m1,-m2},
        {m2,m1},
        {m2,-m1},
        {-m2,m1},
        {-m2,-m1}
    };
    int i,j,u,xx,yy;
    while(!(x.empty())){
        xx=x.front();
        yy=y.front();
        x.pop();
        y.pop();
        for(u=0;u<8;u++){
            i=xx+zh[u][0];
            j=yy+zh[u][1];
            if(i>=1 && i<=n && j>=1 && j<=m && (a[i][j]==1||a[i][j]==4)){
                x.push(i);
                y.push(j);
                a[i][j]=a[xx][yy]+1;
            }
        }
    }
    cout<<a[ans1][ans2];
    return 0;
}

by wuzhoupei @ 2017-09-20 11:19:20

你没有判断是否在队列中;

即inq[][];

cpp

#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
queue <short> x,y;
int n,m,a[405][405];
int inq[1234][1324]; //******************************
int ans1,ans2;
int main(){
    ios::sync_with_stdio(false);
    int m1,m2,bx,by;
    cin>>n>>m>>m1>>m2; 
    for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
                if(a[i][j]==4)
                    ans1=i,ans2=j;
                if(a[i][j]==3)
                    bx=i,by=j;
        }
    }
    x.push(bx);
    y.push(by);
    a[bx][by]=0;
    inq[bx][by]=1;  //************************************
    const int zh[8][2]={
        {m1,m2},
        {m1,-m2},
        {-m1,m2},
        {-m1,-m2},
        {m2,m1},
        {m2,-m1},
        {-m2,m1},
        {-m2,-m1}
    };
    int i,j,u,xx,yy;
    while(!(x.empty())){
        xx=x.front();
        yy=y.front();
        x.pop();
        y.pop();
        for(u=0;u<8;u++){
            i=xx+zh[u][0];
            j=yy+zh[u][1];
            if(i>=1 && i<=n && j>=1 && j<=m && (a[i][j]==1||a[i][j]==4)&&!inq[i][j]){
                x.push(i);
                y.push(j);
                a[i][j]=a[xx][yy]+1;
                inq[i][j]=1; //**************************
            }
        }
    }
    cout<<a[ans1][ans2];
    return 0;
}

|