-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path417.cpp
46 lines (41 loc) · 1.62 KB
/
417.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Author: Tong Xu
// Date: 12/30/2019
// Below solution exceeded time
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
if(matrix.empty()) return {};
m = matrix.size(), n = matrix[0].size();
vector<vector<int>> res;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
vector<vector<int>> visitedPac(m, vector<int>(n, 0));
vector<vector<int>> visitedAt(m, vector<int>(n, 0));
if(dfs(matrix, i, j, visitedPac, 0, 0) && dfs(matrix, i, j, visitedAt, m-1, n-1))
res.push_back({i, j});
}
}
return res;
}
private:
int m;
int n;
bool isvalid(int r, int c) {
return r >= 0 && r < m && c >=0 && c < n;
}
bool dfs(vector<vector<int>>& matrix, int r, int c, vector<vector<int>>& visited, int rowVal, int colVal) {
if(r == rowVal || c == colVal) return true;
if(!isvalid(r, c) || visited[r][c]) return false;
visited[r][c] = 1;
bool ret = false;
if(isvalid(r+1, c) && matrix[r+1][c] <= matrix[r][c])
ret = ret || dfs(matrix, r+1, c, visited, rowVal, colVal);
if(isvalid(r-1, c) && matrix[r-1][c] <= matrix[r][c])
ret = ret || dfs(matrix, r-1, c, visited, rowVal, colVal);
if(isvalid(r, c+1) && matrix[r][c+1] <= matrix[r][c])
ret = ret || dfs(matrix, r, c+1, visited, rowVal, colVal);
if(isvalid(r, c-1) && matrix[r][c-1] <= matrix[r][c])
ret = ret || dfs(matrix, r, c-1, visited, rowVal, colVal);
return ret;
}
};