显示原始代码
#include <bits/stdc++.h>
using namespace std;
int a[301][301];
int s[301][301];
int used[301][301];
int n, m, x, y;
struct point {
int x, y;
};
queue<point> q;
int dx[4] = { 1, -1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
void bfs(int x, int y) {
s[x][y] = 0;
used[x][y] = 1;
point u;
u.x = x, u.y = y;
q.push(u);
while (!q.empty()) {
point k = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
point v;
v.x = k.x + dx[i];
v.y = k.y + dy[i];
if (v.x < 1 || v.x > n || v.y < 1 || v.y > m || a[k.x][k.y] >= a[v.x][v.y] || used[v.x][v.y] == 1)
continue;
s[v.x][v.y] = s[k.x][k.y] + 1;
used[v.x][v.y] = 1;
q.push(v);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << s[i][j] << " ";
cout << endl;
}
}
int main() {
cin >> n >> m >> x >> y;
memset(s, -1, sizeof(s));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cin >> a[i][j];
}
bfs(x, y);
}