-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path417. Pacific Atlantic Water Flow.cpp
32 lines (30 loc) · 1.16 KB
/
417. Pacific Atlantic Water Flow.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
class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<pair<int, int>>res;
if(matrix.empty()) return res;
int m = matrix.size(), n = matrix[0].size();
dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++){
bool reachP = false, reachA = false;
DFS(res, matrix, i, j, m, n, reachP, reachA);
if(reachP && reachA) res.push_back({i, j});
}
return res;
}
void DFS(vector<pair<int, int>>& res, vector<vector<int>>& matrix, int r, int c, int m, int n, bool& reachP, bool& reachA){
if(matrix[r][c] == -1 || reachP && reachA) return;
int tmp = matrix[r][c];
matrix[r][c] = -1;
for(int i = 0; i < 4; i++){
int R = r + dir[i].first, C = c + dir[i].second;
if(R < 0 || C < 0) reachP = true;
else if(R == m || C == n) reachA = true;
else if(matrix[R][C] <= tmp) DFS(res, matrix, R, C, m, n, reachP, reachA);
}
matrix[r][c] = tmp;
}
private:
vector<pair<int, int>>dir;
};