leetcode 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:

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).

给出一个矩阵,每个元素代表一个大陆的高度,水只能向上下左右四个方向流,且只能从高向低流或者流向相同的高度。
矩阵的第一行和第一列代表太平洋( p), 最后一列和最后一行代表大西洋(a),要求输出能同时流向p和a的陆地的index(row, col)

思路:
可以反向思维,分别从太平洋和大西洋的两条边出发,反向找到能到达的点,然后取它们的交集即可。
分别建立太平洋 ( p) 和大西洋(a) 的boolean矩阵作为visited矩阵,寻找点的过程用DFS,从第一行和第一列出发时更新p矩阵,最后一行和最后一列时更新a矩阵。当已经访问过时直接return,而且还有一个条件,就是水只能从高往低流,因此DFS下一个邻居的height < 当前height时直接return。
最后遍历所有点,取p和a同时为true的行列装进结果的list中。

class Solution {
    int rows = 0;
    int cols = 0;
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(matrix == null || matrix.length == 0) {
            return result;
        }
        
        rows = matrix.length;
        cols = matrix[0].length;
        
        boolean[][] p = new boolean[rows][cols];
        boolean[][] a = new boolean[rows][cols];
        int[][] v = new int[rows][cols];
        
        //first and last col
        for(int r = 0; r < rows; r++) {
            dfs(matrix, r, 0, 0, p);
            dfs(matrix, r, cols - 1, 0, a);
        }
        
        //first and last row
        for(int c = 0; c < cols; c++) {
            dfs(matrix, 0, c, 0, p);
            dfs(matrix, rows - 1, c, 0, a);
        }
        
        for(int r = 0; r < rows; r++) {
            for(int c = 0; c < cols; c++) {
                if(p[r][c] && a[r][c]) {
                    List<Integer> tmp = new ArrayList<>();
                    tmp.add(r);
                    tmp.add(c);
                    result.add(tmp);
                }
            }
        }
        
        return result;
    }
    
    public void dfs(int[][] matrix, int r, int c, int height, boolean[][] v){
        if(r < 0 || c < 0 || r >= rows || c >= cols) {
            return;
        }
        
        if(matrix[r][c] < height || v[r][c]) {
            return;
        }
        
        v[r][c] = true;
        dfs(matrix, r, c + 1, matrix[r][c], v);
        dfs(matrix, r, c - 1, matrix[r][c], v);
        dfs(matrix, r + 1, c, matrix[r][c], v);
        dfs(matrix, r - 1, c, matrix[r][c], v);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值