测评信息
#include <iostream>
#include <queue>
#include <cstring>
#include <iomanip>
using namespace std;
const int DIR[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
struct node
{
int x, y;
};
int n, m, x, y;
int ans[401][401] = {};
bool vis[401][401] = {};
queue<node> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(ans, -1, sizeof(ans));
cin >> n >> m >> x >> y;
q.push({x, y});
vis[x][y] = true;
ans[x][y] = 0;
while (!q.empty())
{
node tmp = q.front();
for (int i = 0; i < 8; i++)
{
int nx = tmp.x + DIR[i][0], ny = tmp.y + DIR[i][1];
if (1 <= nx && nx <= n && 1 <= ny && ny <= m && !vis[nx][ny])
{
q.push({nx, ny});
ans[nx][ny] = ans[tmp.x][tmp.y] + 1;
vis[nx][ny] = true;
}
}
q.pop();
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
cout << left << setw(5) << ans[i][j];
cout << "\n";
}
return 0;
}