#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int m, n;
int x, y;
int mm[405][405];
int vis[405][405];
struct cor
{
int x, y, depth;
};
int dir[8][2] = { {2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1} };
queue<cor> q;
void bfs(cor tmpdir)
{
q.push(tmpdir);
while (!q.empty()) {
cor nextCor = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
cor tmpcor;
tmpcor.x = nextCor.x + dir[i][0];
tmpcor.y = nextCor.y + dir[i][1];
tmpcor.depth = nextCor.depth + 1;
if (tmpcor.x >= 0 && tmpcor.x < n && tmpcor.y >= 0 && tmpcor.y < m&&!vis[tmpcor.x][tmpcor.y]) {
mm[tmpcor.x][tmpcor.y] = tmpcor.depth;
vis[tmpcor.x][tmpcor.y] = 1;
q.push(tmpcor);
}
}
}
}
int main()
{
cin >> n >> m >> x >> y;
cor tmp;
tmp.x = x-1;
tmp.y = y-1;
tmp.depth = 0;
vis[x-1][y-1] = 1;
bfs(tmp);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 0) {
mm[i][j] = -1;
}
printf("%-5d",mm[i][j]);
}
cout << endl;
}
return 0;
}
#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int m, n;
int x, y;
int mm[405][405];
int vis[405][405];
struct cor
{
int x, y, depth;
};
int dir[8][2] = { {2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1} };
queue<cor> q;
void bfs(cor tmpdir)
{
q.push(tmpdir);
while (!q.empty()) {
cor nextCor = q.front();
mm[nextCor.x][nextCor.y] = nextCor.depth;
vis[nextCor.x][nextCor.y] = 1;
q.pop();
for (int i = 0; i < 8; i++) {
cor tmpcor;
tmpcor.x = nextCor.x + dir[i][0];
tmpcor.y = nextCor.y + dir[i][1];
tmpcor.depth = nextCor.depth + 1;
if (tmpcor.x >= 0 && tmpcor.x < n && tmpcor.y >= 0 && tmpcor.y < m&&!vis[tmpcor.x][tmpcor.y]) {
q.push(tmpcor);
}
}
}
}
int main()
{
cin >> n >> m >> x >> y;
cor tmp;
tmp.x = x-1;
tmp.y = y-1;
tmp.depth = 0;
bfs(tmp);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 0) {
mm[i][j] = -1;
}
printf("%-5d",mm[i][j]);
}
cout << endl;
}
return 0;
}