蒟蒻全WA求助

P1746 离开中山路

DWDWDWDW @ 2020-08-27 15:21:56

#include <iostream>
using namespace std;
int n,head=1,tail=2,startx,starty,endx,endy,book[1001][1001];
int xx[]={1,0,-1,0};
int yy[]={0,-1,0,1};
char k[1001][1001];
int mp[1001][1001];
struct note{
    int x,y,step;
}que[5000001];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>k[i][j];
            if(k[i][j]=='1') mp[i][j]=1;
            else mp[i][j]=0;
        }
    }
    cin>>startx>>starty>>endx>>endy;
    que[head].x=startx;
    que[head].y=starty;
    que[head].step=0;
    book[startx][starty]=1;
    while(head<tail){
        for(int i=0;i<4;i++){
            int tx=que[head].x+xx[i];
            int ty=que[head].y+yy[i];
            if(tx>0 && tx<=n && ty>0 && ty<=n && mp[tx][ty]==0){
                if(!book[tx][ty]){
                    book[tx][ty]=1;
                    que[tail].x=tx;
                    que[tail].y=ty;
                    que[tail].step=que[head].step+1;
                    tail++;
                    if(tx==endx && ty==endy) break;
                }
            }
        }
        head++;
    }
    cout<<que[tail-1].step;
    return 0;
}

by DWDWDWDW @ 2020-08-27 16:27:23

各位大佬过来帮帮忙吧


by 沉鸣cmh @ 2020-09-23 20:37:37

帮你改完了

#include <iostream>
using namespace std;
int n,head=0,tail=1,startx,starty,endx,endy,book[1001][1001];
int xx[5]={1,0,-1,0};
int yy[5]={0,-1,0,1};
char k;
int mp[1001][1001];
struct note{
    int x,y,step;
}que[5000001];
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>k;
            if(k=='1') mp[i][j]=1;
        }
    }
    cin>>startx>>starty>>endx>>endy;
    que[1].x=startx;
    que[1].y=starty;
    mp[startx][starty]=1;
    while(head<tail){
        head++;
        for(int i=0;i<4;i++){
            int tx=que[head].x+xx[i];
            int ty=que[head].y+yy[i];
            if(tx>0 && tx<=n && ty>0 && ty<=n && !mp[tx][ty]){
                    tail++;
                    mp[tx][ty]=1;
                    que[tail].x=tx;
                    que[tail].y=ty;
                    que[tail].step=que[head].step+1;
                    if(tx==endx && ty==endy){cout<<que[tail].step;return 0;}
            }
        }
    }
    cout<<que[tail-1].step;
    return 0;
}

|