17. Pacific Atlantic Water Flow

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:

  1. The order of returned grid coordinates does not matter.
  2. 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 {
    boolean pacific = false, atlantic = false;
    public List<int[]> pacificAtlantic(int[][] matrix) {
        int row = matrix.length;
        List<int[]> result = new ArrayList<>();
        if (row == 0){
            return result;
        }
        int col = matrix[0].length;
        int[][] cach = new int[row][col];
        int[][] visited = new int[row][col];
        for (int i = 0; i < row; ++ i){
            for (int j = 0; j < col; ++ j){
                pacific = false;
                atlantic = false;
                dfs(matrix, cach, visited, i, j);
                if (pacific&&atlantic){
                    cach[i][j] = 3;
                }
                else if (pacific){
                    cach[i][j] = 1;
                }
                else if (atlantic){
                    cach[i][j] = 2;
                }
                if (pacific&&atlantic){
                    int[] t = new int[]{i, j};
                    result.add(t);
                }
            }
            System.out.println();
        }
        return result;
    }
    public void dfs(int[][] matrix, int[][] cach, int[][] visited, int i, int j){
        if (i >= matrix.length || j >= matrix[0].length || i < 0 || j < 0){
            return;
        }
        if ((i == matrix.length - 1)||(j == matrix[0].length - 1)){
            atlantic = true;
        }
        if ((i == 0)||(j == 0)){
            pacific = true;
        }
        if (atlantic&&pacific){
            return;
        }
        if (cach[i][j] == 1){
            pacific = true;
        }
        if (cach[i][j] == 2){
            atlantic = true;
        }
        if (cach[i][j] == 3){
            pacific = true;
            atlantic = true;
            return;
        }
        if (visited[i][j] == 1){
            return;
        }
        visited[i][j] = 1;
        if (i + 1 < matrix.length&&matrix[i+1][j] <= matrix[i][j]){
            dfs(matrix, cach, visited, i + 1, j);
        }
        if (i - 1 >= 0 && matrix[i-1][j] <= matrix[i][j]){
            dfs(matrix, cach, visited, i - 1, j);
        }
        if (j+1 < matrix[0].length && matrix[i][j+1] <= matrix[i][j]){
            dfs(matrix, cach, visited, i, j + 1);
        }
        if (j - 1 >= 0 && matrix[i][j-1] <= matrix[i][j]){
            dfs(matrix, cach, visited, i, j - 1);
        }
        visited[i][j] = 0;
    }
}



扫描二维码关注公众号,回复: 1947609 查看本文章


猜你喜欢

转载自blog.csdn.net/excellentlizhensbfhw/article/details/80906387