https://www.luogu.com.cn/problem/P1443
// Problem:
// P1443 马的遍历
//
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1443
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
const int N = 405;
int n, m, x, y;
int dist[N][N];
int vis[N][N];
int dx[] = {2, 2, 1, -1, -2, -2, -1, 1};
int dy[] = {-1, 1, 2, 2, 1, -1, -2, -2};
void bfs() {
queue<pair<int, int> > q;
dist[x][y] = 0;
vis[x][y] = 1;
q.push({x, y});
int cnt = 1;
while (q.size()) {
// cout << q.size() << endl;
pair<int, int> u = q.front();
q.pop();
for (int i=0;i<8;i++) {
int nx = u.first+dx[i];
int ny = u.second+dy[i];
if (nx<=0||ny<=0||nx>n||ny>m) continue ;
if (dist[nx][ny] == -1 && vis[nx][ny] == 0) {
dist[nx][ny] = cnt;
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
// cout << cnt << endl;
cnt++;
}
}
int main () {
cin >> n >> m >> x >> y;
for (int i=0;i<=n;i++) {
for (int j=0;j<=m;j++) {
dist[i][j] = -1;
}
}
bfs();
for(int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
cout << dist[i][j] << ' ';
}
cout << endl;
}
return 0;
}