#8WA求调!!!

P1443 马的遍历

LabmemNo_012LzTopic @ 2024-07-26 15:11:11

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

struct ll
{
    int a[405];
    int b[405];
    int sum = 0;
}bs[405];

int map[405][405], n, m, x, y, tiao[17] = { 0,2, 1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2 }, k, xl, yl, q, e;
void BFS(int l)
{
    if (bs[l].sum == 0)return;
    for (int i = 1; i <= bs[l].sum; i++)
    {
        q = bs[l].a[i];
        e = bs[l].b[i];
        for (int j = 1; j <= 8; j++)
        {
            k = 2 * j;
            xl = q + tiao[k - 1];
            yl = e + tiao[k];
            if (map[xl][yl] == -1)
                    {
                        bs[l + 1].sum++;
                        bs[l + 1].a[bs[l + 1].sum] = xl;
                        bs[l + 1].b[bs[l + 1].sum] = yl;
                        map[xl][yl] = l + 1;
                    }
        }
    }
    BFS(l + 1);
}

int main() 
{
    cin >> n >> m >> x >> y;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            map[i][j] = -1;
        }
    }
    map[x][y] = 0;
    bs[0].sum = 1;
    bs[0].a[1] = x;
    bs[0].b[1] = y;
    BFS(0);
    for (int i = 1; i <= n; i++)
    {
        if (i != 1)printf("\n");
        for (int j = 1; j <= m; j++)
        {
            printf("%d ", map[i][j]);
        }
    }
    return 0;
}

|