417. 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解题。初始化两个boolean数组Pacific和Atlantic分别表示此节点能够到达Pacific或者Atlantic,那么分别从第一行和第一列开始递归更新能够到达Pacific的数组,到达边缘或者当前值比height低,则返回,不能到达这个点,否则,记录这个点为true能够到达,继续四向遍历。同理从最后一行和最后一列开始遍历,得到能够到达Atlantic的数组。最后遍历两个数组,如果都为true,记录这个点,返回res。代码如下:

public class Solution {
    public List<int[]> pacificAtlantic(int[][] matrix) {
        int m = matrix.length;
        List<int[]> res = new ArrayList<int[]>();
        if (m == 0) {
            return res;
        }
        int n = matrix[0].length;
        boolean[][] Pacific = new boolean[m][n];
        boolean[][] Atlantic = new boolean[m][n];
        for (int i = 0; i < m; i ++) {
            checkFlow(Pacific, matrix, i, 0, Integer.MIN_VALUE);
            checkFlow(Atlantic, matrix, i, n - 1, Integer.MIN_VALUE);
        }
        for (int i = 0; i < n; i ++) {
            checkFlow(Pacific, matrix, 0, i, Integer.MIN_VALUE);
            checkFlow(Atlantic, matrix, m - 1, i, Integer.MIN_VALUE);
        }
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                if (Pacific[i][j] && Atlantic[i][j]) {
                    res.add(new int[]{i, j});
                }
            }
        }
        return res;
    }
    
    private void checkFlow(boolean[][] canReach, int[][] matrix, int i, int j, int height) {
        int m = matrix.length;
        int n = matrix[0].length;
        if (i < 0 || j < 0 || i >= m || j >= n || canReach[i][j] || matrix[i][j] < height) {
            return;
        }
        canReach[i][j] = true;
        checkFlow(canReach, matrix, i + 1, j, matrix[i][j]);
        checkFlow(canReach, matrix, i - 1, j, matrix[i][j]);
        checkFlow(canReach, matrix, i, j + 1, matrix[i][j]);
        checkFlow(canReach, matrix, i, j - 1, matrix[i][j]);
    }
}
另一种递归方案是,只有在数组内部而且此点没有被遍历过,而且此点比周围某个方向的点的height高的时候才继续这个方向的遍历,并且设置这个点为遍历过true,比上一种方法快。代码如下:
static int[] dx = {-1,0,0,1};
static int[] dy = {0,1,-1,0};
public List<int[]> pacificAtlantic(int[][] matrix) {
    List<int[]> res = new ArrayList<>();
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;
    boolean[][] pacific = new boolean[matrix.length][matrix[0].length];
    boolean[][] atlantic = new boolean[matrix.length][matrix[0].length];
    for (int i = 0; i < matrix.length; i++){
        pacific[i][0] = true;
        atlantic[i][matrix[0].length-1] = true;
    }
    for (int j = 0; j < matrix[0].length; j++){
        pacific[0][j] = true;
        atlantic[matrix.length-1][j] = true;
    }
    for (int i = 0; i < matrix.length; i++){
        explore(pacific, matrix, i, 0);
        explore(atlantic, matrix, i, matrix[0].length-1);
    }
    for (int j = 0; j < matrix[0].length; j++){
        explore(pacific, matrix, 0, j);
        explore(atlantic, matrix, matrix.length-1, j);
    }
    for (int i = 0; i < matrix.length; i++){
        for (int j = 0; j < matrix[0].length; j++){
            if (pacific[i][j] && atlantic[i][j] == true)
                res.add(new int[]{i,j});
        }
    }
    return res;
    
}
private void explore(boolean[][] grid, int[][] matrix, int i, int j){
    grid[i][j] = true;
    for (int d = 0; d < dx.length; d++){
        if (i+dy[d] < grid.length && i+dy[d] >= 0 && 
            j + dx[d] < grid[0].length && j + dx[d] >= 0 && 
            grid[i+dy[d]][j+dx[d]] == false && matrix[i+dy[d]][j+dx[d]] >= matrix[i][j])
                explore(grid, matrix, i+dy[d], j+dx[d]);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值