题:https://leetcode.com/problems/pacific-atlantic-water-flow/description/
题目
Given an m 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).
题目大意
矩阵的左边和上边是太平洋,右边和下边是大西洋,矩阵中的数字代表每个点的海拔,高海拔的水可以流向同海拔或低海拔地方。
求能同时流到太平洋和大西洋的所有位置。
思路
从四个边界开始反向DFS遍历,能留到该点的上一个点。
通过两个 布尔向量来 分别记录能 留到 太平洋和大西洋的点。
class Solution {
int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}};
public void explore(int[][] matrix,boolean[][] blETouch,int r,int c){
if(blETouch[r][c])
return ;
blETouch[r][c] = true;
for(int i = 0 ; i < directions.length ;i++){
int nr = r+directions[i][0];
int nc = c+directions[i][1];
if((nr>=0 && nr<matrix.length && nc >=0 && nc<matrix[0].length) && (matrix[nr][nc]>=matrix[r][c]))
explore(matrix,blETouch,nr,nc);
}
}
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int[]> res = new ArrayList<>();
if(matrix.length == 0)
return res;
boolean[][][] blTouch = new boolean[2][matrix.length][matrix[0].length];
for(int i = 0 ;i < matrix.length;i++){
explore(matrix,blTouch[0],i,0);
explore(matrix,blTouch[1],i,matrix[0].length-1);
}
for(int j =0 ; j< matrix[0].length;j++){
explore(matrix,blTouch[0],0,j);
explore(matrix,blTouch[1],matrix.length-1,j);
}
for(int i = 0 ;i < matrix.length;i++)
for(int j =0 ; j< matrix[0].length;j++){
if(blTouch[0][i][j] && blTouch[1][i][j] ){
res.add(new int[]{i,j});
}
}
return res;
}
}
ps:
DFS 、 BFS 和 递归不一样,自己感觉。
递归我觉得是问题分解的一种方式,类似 动态规划。将大问题转化为小问题,而 DFS 与 BFS 是面对 图问题的 一种遍历方式。
这道题写了 一个递归的方式,但是 错误的,因为 是从一点 看它周围有没有点 是 能流到海的,不对是因为 周围的点 可能 刚好只能 依靠 本点来 流至海。
错误代码
class Solution {
boolean[][][] blVisited;
boolean[][][] blTouch;
int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}};
public boolean explore(int[][] matrix,int d,int r,int c){
if(blVisited[d][r][c])
return blTouch[d][r][c];
blVisited[d][r][c] = true;
if(d == 0){
if(r == 0 || c == 0)
blTouch[d][r][c] = true;
}else {
if(r == matrix.length-1 || c == matrix[0].length-1)
blTouch[d][r][c] = true;
}
for(int i = 0 ; i < directions.length && blTouch[d][r][c]!=true;i++){
int nr = r+directions[i][0];
int nc = c+directions[i][1];
if((nr>=0 && nr<matrix.length && nc >=0 && nc<matrix[0].length) && (matrix[nr][nc]<=matrix[r][c]))
blTouch[d][r][c] |= explore(matrix,d,nr,nc);
}
return blTouch[d][r][c];
}
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int[]> res = new ArrayList<>();
if(matrix.length == 0)
return res;
blVisited = new boolean[2][matrix.length][matrix[0].length];
blTouch = new boolean[2][matrix.length][matrix[0].length];
for(int i = 0 ;i < matrix.length;i++)
for(int j =0 ; j< matrix[0].length;j++){
if(explore(matrix,0,i,j) && explore(matrix,1,i,j) ){
res.add(new int[]{i,j});
}
}
return res;
}
}