map-of-highest-peak 1.0.0
Map of Highest Peak
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

vector< vector< int > > highestPeak (vector< vector< int > > &isWater)
 

Detailed Description

Definition at line 5 of file main.cpp.

Member Function Documentation

◆ highestPeak()

vector< vector< int > > Solution::highestPeak ( vector< vector< int > > &  isWater)
inline

Definition at line 8 of file main.cpp.

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 }

Referenced by main().


The documentation for this class was generated from the following file: