#include <bits/stdc++.h>
using namespace std;
int n, m, s[600][600], x, y;
int fx[8][2] = {{-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2}, {-2, 1}};
queue<pair<int, int>> q;
void bfs()
{
q.push({x, y});
s[x][y] = 1;
while (!q.empty())
{
pair<int, int> u = q.front();
q.pop();
for (int i = 0; i < 8; i++)
{
int nextx = u.first + fx[i][0], nexty = u.second + fx[i][1];
if (nextx >= 0 && nextx < n && nexty >= 0 && nexty < m && !s[nextx][nexty])
{
q.push({nextx, nexty});
s[nextx][nexty] = s[u.first][u.second] + 1;
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> x >> y;
x--, y--;
bfs();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << s[i][j] - 1 << " ";
}
cout << endl;
}
}
洛谷马的遍历
最新推荐文章于 2024-09-27 11:26:03 发布