Re: [闲聊] 每日leetcode

楼主: dont   2025-01-22 22:16:06
1765. Map of Highest Peak
## 思路
水是0, 相邻的点最多差1
所以从水开始BFS
## Code
```cpp
class Solution {
public:
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
int lenR = isWater.size(), lenC = isWater[0].size();
vector<pair<int, int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> height(lenR, vector<int>(lenC, -1));
queue<pair<int, int>> q;
for (int r=0; r<lenR; ++r) {
for (int c=0; c<lenC; ++c) {
if (isWater[r][c]) {
q.push({r, c});
height[r][c] = 0;
}
}
}
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
for (auto [dr, dc] : dirs) {
int nextR = r + dr, nextC = c + dc;
if (0 <= nextR && nextR < lenR && 0 <= nextC && nextC < lenC
&& height[nextR][nextC] == -1) {
q.push({nextR, nextC});
height[nextR][nextC] = height[r][c] + 1;
}
}
}
return height;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com