417. Pacific Atlantic Water Flow
Description
Given anm x n
matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.
Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.
Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.
Note:
- The order of returned grid coordinates does not matter.
- Both m and n are less than 150.
Example:
Given the following 5x5 matrix:
Pacific ~ ~ ~ ~ ~
~ 1 2 2 3 (5) *
~ 3 2 3 (4) (4) *
~ 2 4 (5) 3 1 *
~ (6) (7) 1 4 5 *
~ (5) 1 1 2 4 *
* * * * * Atlantic
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
Discussion
Solution 1
dfs, tracker[m][n], visited_pacific[m][n], visited_atlantic[m][n].
for every matrix[y][x] >= matrix[preY][preX], tracker[y][x] -= 1;
dfs from out-most points.
if tracker[y][x] == -2, it is the candidate.
class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<pair<int, int>> rst;
if (0 == matrix.size())
return rst;
vector<vector<int>> tracker(matrix.size(), vector<int>(matrix[0].size(), 0));
vector<vector<bool>> visted_pacific(matrix.size(), vector<bool>(matrix[0].size(), false));
vector<vector<bool>> visted_atlantic(matrix.size(), vector<bool>(matrix[0].size(), false));
for(int y = 0; y < matrix.size(); ++y) {
dfs(tracker, matrix, visted_pacific, y, 0);
dfs(tracker, matrix, visted_atlantic, y, matrix[0].size() - 1);
}
/*
for(int y = 0; y < matrix.size(); ++y) {
for(int x = 0; x < matrix[0].size(); ++x) {
visted_pacific[y][x] = false;
visted_atlantic[y][x] = false;
}
}
*/
for(int x = 0; x < matrix[0].size(); ++x) {
dfs(tracker, matrix, visted_pacific, 0, x);
dfs(tracker, matrix, visted_atlantic, matrix.size() - 1, x);
}
#ifdef TEST
for(int y= 0; y < matrix.size(); ++y) {
copy(tracker[y].begin(), tracker[y].end(), ostream_iterator<int>(cout, ", "));
cout << endl;
}
cout << "test" << endl;
#endif
for(int y = 0; y < tracker.size(); ++y) {
for(int x = 0; x < tracker[0].size(); ++x) {
if(-2 == tracker[y][x]) {
rst.emplace_back(make_pair(y, x));
}
//cout << y << "," << x << endl;
}
}
//cout << "test" << endl;
#ifdef TEST
for(auto each : rst) {
cout << "[" << each.first << ", " << each.second << "]" << endl;
}
#endif
return rst;
}
private:
void dfs(vector<vector<int>>& tracker, vector<vector<int>>& matrix,
vector<vector<bool>>& visited,
int y, int x) {
vector<int> dy = {0,0,1,-1};
vector<int> dx = {1,-1,0,0};
if(visited[y][x] == true)
return;
tracker[y][x] -= 1;
visited[y][x] = true;
for(int loop = 0; loop < dy.size(); ++loop) {
if(y + dy[loop] >= 0 && y + dy[loop] < tracker.size() &&
x + dx[loop] >= 0 && x + dx[loop] < tracker[0].size() &&
visited[y+dy[loop]][x+dx[loop]] == false &&
matrix[y+dy[loop]][x+dx[loop]] >= matrix[y][x]) {
dfs(tracker, matrix, visited, y + dy[loop], x + dx[loop]);
}
}
}
};