1 条题解

  • -1
    @ 2023-3-30 22:46:13

    #include <bits/stdc++.h>

    using namespace std;

    //#define knight 3

    struct xy{

    int x,y;

    }node,Top;

    const int dx[4]={1,-1,2,-2};

    const int dy[4]={1,-1,2,-2};

    int a[401][401];

    bool b[401][401];

    int n,m;

    void bfs(int x,int y,int step){

    a[x][y] = step;

    b[x][y] = false;

    queue Q;

    node.x = x;

    node.y = y;

    Q.push(node);

    while (!Q.empty()){

    Top=Q.front();

    Q.pop();

    for (int i=0;i<4;i++)

    for (int j=0;j<4;j++)

    if (abs(dx[i])!=abs(dy[j])){

    int NewX=Top.x+dx[i];

    int NewY=Top.y+dy[j];

    if (NewX<1||NewX>n||NewY<1||NewY>m) continue;

    if (b[NewX][NewY]){

    node.x=NewX;

    node.y=NewY;

    Q.push(node);

    b[NewX][NewY] = false;

    a[NewX][NewY] = a[Top.x][Top.y]+1;

    }

    }

    }

    }

    int main(){

    memset(b,true,sizeof(b));

    memset(a,-1,sizeof(a));

    int x,y;

    scanf("%d%d%d%d" ,&n ,&m ,&x ,&y );

    bfs(x,y,0);

    for (int i=1;i<=n;i++){

    for (int j=1;j<=m;j++)

    printf("%-5d", a[i][j]);

    printf("\n");

    }

    return 0;

    }

    • 1

    信息

    ID
    1309
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    195
    已通过
    21
    上传者