显示原始代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 300 + 5;
int ht[N][N], dis[N][N];
pair<int, int> mv[4] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
queue<pair<int, int>> que;
int read() {
int x = 0, s = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
s = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
return x * s;
}
int main() {
#ifdef LOCAL
freopen("c.in", "r", stdin);
#endif
int n = read(), m = read();
int x = read(), y = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) ht[i][j] = read();
memset(dis, -1, sizeof(dis));
dis[x][y] = 0, que.push({ x, y });
while (!que.empty()) {
int nowx = que.front().first, nowy = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int nx = nowx + mv[i].first, ny = nowy + mv[i].second;
if (nx > n || nx < 1 || ny > m || ny < 1)
continue;
if (dis[nx][ny] == -1 && ht[nx][ny] > ht[nowx][nowy]) {
dis[nx][ny] = dis[nowx][nowy] + 1;
que.push({ nx, ny });
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << dis[i][j] << ' ';
cout << endl;
}
return 0;
}