大佬我这里哪错了qwq
查看原帖
大佬我这里哪错了qwq
757092
bj12z_wangziqian楼主2022/10/26 22:32
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define N 410
int a[410][410];
bool b[N][N];
struct bfss
{
    int x,y;
};

int dx[8]={-2,-2,2,2,1,-1,1,-1};
int dy[8]={-1,1,-1,1,2,-2,-2,2};
int m,n;
queue <bfss> q;
bool check(int x,int y)
{
    if(x>=1&&x<=m&&y>=1&&y<=n&&a[x][y]==-1)
        return true;
    else
        return false;
}
void bfs(int x,int y,int step){
    a[x][y] = step;
    b[x][y] = false;
    bfss n;
    n.x = x;
    n.y = y;
    q.push(n);
    bfss t;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        for (int i=0;i<=8;i++)
        {
            int nx=t.x+dx[i];
            int ny=t.y+dy[i];
            if (check(nx,ny)&&b[nx][nx])
            {
                n.x=nx;
                n.y=ny;
                q.push(n);
                b[nx][ny] = false;
                a[nx][ny] = a[t.x][t.y]+1;
            }

        }


    }
}
int main()
{
    for(int i = 0;i<=410;i++)
    {
        for(int j = 0;j<=410;j++)
        {
            a[i][j] = -1;
        }
    }
    int x,y;
    cin >> n >> m >> x >> y;
    bfs(x,y,0);
    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=m;j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
2022/10/26 22:32
加载中...