diff --git a/graph_lectures/pacific_atlantic_water_flow.cpp b/graph_lectures/pacific_atlantic_water_flow.cpp index 2062953..a7c8b3b 100644 --- a/graph_lectures/pacific_atlantic_water_flow.cpp +++ b/graph_lectures/pacific_atlantic_water_flow.cpp @@ -10,6 +10,30 @@ class Solution { int cols; vector > h; + + + vector > bfs(queue > &qu) { + vector > visited(rows, vector (cols, false)); + while(not qu.empty()) { + auto cell = qu.front(); + qu.pop(); + int i = cell.first; + int j = cell.second; + visited[i][j] = true; + for(int d = 0; d < 4; d++) { + int newRow = i+dir[d][0]; + int newCol = j+dir[d][1]; + if(newRow < 0 or newCol < 0 or newRow >= rows or newCol >= cols) continue; // you exited the grid + if(visited[newRow][newCol]) continue; + if(h[newRow][newCol] < h[i][j]) continue; // h[newRow][newCol] -> neighbours height, h[i][j] -> curr cell's heigh + qu.push({newRow, newCol}); + } + } + return visited; + } + + + vector> pacificAtlantic(vector>& heights) { rows = heights.size(); cols = heights[0].size(); @@ -42,28 +66,8 @@ class Solution { } return result; } - - vector > bfs(queue > &qu) { - vector > visited(rows, vector (cols, false)); - while(not qu.empty()) { - auto cell = qu.front(); - qu.pop(); - int i = cell.first; - int j = cell.second; - visited[i][j] = true; - for(int d = 0; d < 4; d++) { - int newRow = i+dir[d][0]; - int newCol = j+dir[d][1]; - if(newRow < 0 or newCol < 0 or newRow >= rows or newCol >= cols) continue; // you exited the grid - if(visited[newRow][newCol]) continue; - if(h[newRow][newCol] < h[i][j]) continue; // h[newRow][newCol] -> neighbours height, h[i][j] -> curr cell's heigh - qu.push({newRow, newCol}); - } - } - return visited; - } }; int main() { return 0; -} \ No newline at end of file +}