-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapofHighestPeak.cpp
More file actions
35 lines (34 loc) · 1.06 KB
/
mapofHighestPeak.cpp
File metadata and controls
35 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Source: https://leetcode.com/problems/map-of-highest-peak/
// Author: Miao Zhang
// Date: 2021-06-04
class Solution {
public:
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
int m = isWater.size();
int n = isWater[0].size();
vector<vector<int>> res(m, vector<int>(n, -1));
queue<pair<int, int>> q;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (isWater[i][j]) {
q.emplace(i, j);
res[i][j] = 0;
}
}
}
vector<vector<int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (auto d: dirs) {
int x = i + d[0];
int y = j + d[1];
if (x < 0 || x >= m || y < 0 || y >= n) continue;
if (res[x][y] != -1) continue;
res[x][y] = res[i][j] + 1;
q.emplace(x, y);
}
}
return res;
}
};