求助,样例能过,提交全WA

P1746 离开中山路

XXCCVV @ 2022-07-16 14:09:17

#include<iostream>
#include<queue>
using namespace std;
struct st {
    int x,y,step;
} a;
char map[1005][1005];int vis[1005][1005];
int n,x1,y1,x2,y2;
short dex[4]={0,1,0,-1};
short dey[4]={1,0,-1,0};
void bfs() {
    queue<st>q;
    q.push(a);
    while(!q.empty()) {
        st temp=q.front();
        q.pop();
        if(temp.x==x2&&temp.y==y2) {
            cout<<temp.step;
            return;
        }
        for(int i=0;i<4;i++){
            int dx=temp.x+dex[i];
            int dy=temp.y+dey[i];
            if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&map[dx][dy]=='0'&&!vis[dx][dy]){
                st tt={dx,dy,temp.step+1};
                vis[dx][dy]=1;
                q.push(tt);
            }
        }
    }
}
int main() {
    cin>>n;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) {
            cin>>map[i][j];
        }
    }
    cin>>x1>>y1>>x2>>y2;
    a.x=a.y=1;
    a.step=0;
    vis[1][1]=1;
    bfs();
    return 0;
}

by OutsideR_ @ 2022-07-16 14:13:32

@XXCCVV

a.x=a.y=1;

这一行的问题,起始位置可能不是(1,1)


by OutsideR_ @ 2022-07-16 14:15:13

@XXCCVV

a.x=a.y=1;
a.step=0;
vis[1][1]=1;

修改为

a.x=x1;
a.y=y1;
a.step=0;
vis[x1][y1]=1;

就可以了


by OutsideR_ @ 2022-07-16 14:16:38

@XXCCVV 起始位置是(x1,y1)不是(1,1)


by XXCCVV @ 2022-07-16 15:40:58

@Cyc曹 谢谢大佬


|