map-of-highest-peak
1.0.0
Map of Highest Peak
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
5
class
Solution
6
{
7
public
:
8
vector<vector<int>>
highestPeak
(vector<vector<int>>& isWater)
9
{
10
int
m = (int)isWater.size();
11
int
n = (int)isWater[0].size();
12
vector<vector<int>> height(m, vector<int>(n, -1));
13
queue<pair<int, int>> q;
14
for
(
int
i = 0; i < m; ++i)
15
for
(
int
j = 0; j < n; ++j)
16
if
(isWater[i][j] == 1)
17
{
18
q.push({i, j});
19
height[i][j] = 0;
20
}
21
while
(!q.empty())
22
{
23
auto
[i, j] = q.front();
24
q.pop();
25
int
val = height[i][j] + 1;
26
if
(i - 1 >= 0 && height[i - 1][j] == -1)
27
{
28
height[i - 1][j] = val;
29
q.push({i - 1, j});
30
}
31
if
(i + 1 < m && height[i + 1][j] == -1)
32
{
33
height[i + 1][j] = val;
34
q.push({i + 1, j});
35
}
36
if
(j - 1 >= 0 && height[i][j - 1] == -1)
37
{
38
height[i][j - 1] = val;
39
q.push({i, j - 1});
40
}
41
if
(j + 1 < n && height[i][j + 1] == -1)
42
{
43
height[i][j + 1] = val;
44
q.push({i, j + 1});
45
}
46
}
47
return
height;
48
}
49
};
50
51
int
main
()
52
{
53
vector<vector<int>> water = {{0,0,1},{1,0,0},{0,0,0}};
54
vector<vector<int>> height =
Solution
().
highestPeak
(water);
55
for
(vector<int>& row: height)
56
{
57
for
(
int
c: row)
58
cout << c <<
" "
;
59
cout << endl;
60
}
61
return
0;
62
}
Solution
Definition
main.cpp:6
Solution::highestPeak
vector< vector< int > > highestPeak(vector< vector< int > > &isWater)
Definition
main.cpp:8
main
int main()
Definition
main.cpp:51
main.cpp
Generated by
1.9.8