原题链接:https://leetcode-cn.com/problems/map-of-highest-peak/
多源bfs
class Solution {
public:
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
int m = isWater.size(), n = isWater[0].size();
vector<vector<int>> vec(m, vector<int>(n, -1));
queue<pair<int, int>> que;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (isWater[i][j] == 1) {
vec[i][j] = 0;
que.push({i, j});
}
}
}
while (que.size()) {
auto p = que.front();
int i = p.first, j = p.second;
for (int k = 0; k < 4; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x < 0 || y < 0 || x >= m || y >= n || vec[x][y] != -1 ) continue;
vec[x][y] = vec[i][j] + 1;
que.push({x, y});
}
que.pop();
}
return vec;
}
};