-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map of Highest Peak.cpp
38 lines (38 loc) · 1.2 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
class Solution {
public:
vector<vector<int>> highestPeak(vector<vector<int>>& mat) {
int n = mat.size();
int m = mat[0].size();
vector<vector<int>> ans(n, vector<int>(m, 0));
vector<vector<int>> vis(n, vector<int>(m, 0));
queue<pair<pair<int, int>, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == 1) {
vis[i][j] = 1;
q.push({{i, j}, 0});
}
}
}
while (!q.empty()) {
pair<pair<int, int>, int> p = q.front();
q.pop();
int r = p.first.first;
int c = p.first.second;
int h = p.second;
int x[] = {-1, 0, 1, 0};
int y[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int nx = r + x[i];
int ny = c + y[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
vis[nx][ny] == 0) {
ans[nx][ny] = h + 1;
vis[nx][ny] = 1;
q.push({{nx, ny}, h + 1});
}
}
}
return ans;
}
};