稻花香里说丰年,听取WA声一片

P1443 马的遍历

buoluo_1 @ 2024-07-30 11:17:56

2~9测试点全WA了,求助

#include<bits/stdc++.h>
#define ll long long
#define str string
using namespace std;
struct node{
    int x,y,step;
};
const int N=410;
int f[N][N];
int n,m,hx,hy;
int fx[8][2]={{-2 -1},{-2,1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
queue<node> q;
bool check(int newx,int newy) {
    return newx>=1&&newx<=n&&newy>=1&&newy<=m;
}
int main(){
    memset(f,-1,sizeof f);
    scanf("%d%d%d%d",&n,&m,&hx,&hy);
    q.push((node){hx,hy,0});
    f[hx][hy]=0;
    while(q.size()){
        node now=q.front();
        q.pop();
        for(int i=0;i<8;i++){
            int newx=now.x+fx[i][0],newy=now.y+fx[i][1];
            if(check(newx,newy)&&f[newx][newy]==-1||(check(newx,newy)&&(now.step+1<f[newx][newy]))) {
                q.push({newx,newy,now.step+1});
                f[newx][newy]=now.step+1;
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            printf("%-5d",f[i][j]);
        }
        printf("\n");
    }
    return 0;
}

by meifan666 @ 2024-07-30 11:21:59

%-5d改%5d,第一列不用空出


|