Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions graph_lectures/pacific_atlantic_water_flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ class Solution {
int cols;
vector<vector<int> > h;



vector<vector<bool> > bfs(queue<pair<int, int> > &qu) {
vector<vector<bool> > visited(rows, vector<bool> (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<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
rows = heights.size();
cols = heights[0].size();
Expand Down Expand Up @@ -42,28 +66,8 @@ class Solution {
}
return result;
}

vector<vector<bool> > bfs(queue<pair<int, int> > &qu) {
vector<vector<bool> > visited(rows, vector<bool> (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;
}
}