-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathMap of Highest Peak.cpp
44 lines (38 loc) · 1.49 KB
/
Map of Highest Peak.cpp
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
36
37
38
39
40
41
42
43
44
class Solution {
public:
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
int m = isWater.size();
int n = isWater.front().size();
vector<vector<int>> result(m, vector<int>(n, -1));
queue<pair<int, int>> myqueue;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (isWater[i][j] == 1) {
myqueue.push({i, j});
result[i][j] = 0;
}
}
}
int cur_height = 0;
while (!myqueue.empty()) {
int total = myqueue.size();
for (int k = 0; k < total; k++) {
pair<int, int> pos = myqueue.front();
myqueue.pop();
pair<int, int> directions[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (pair<int, int> direction : directions) {
int new_pos_x = pos.first + direction.first;
int new_pos_y = pos.second + direction.second;
if (new_pos_x < 0 || new_pos_x >= m|| new_pos_y < 0 || new_pos_y >= n
|| result[new_pos_x][new_pos_y] != -1) {
continue;
}
myqueue.push({new_pos_x, new_pos_y});
result[new_pos_x][new_pos_y] = cur_height + 1;
}
}
cur_height++;
}
return result;
}
};