同时被两种海水经过的点的坐标 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).

解决:

① 水可以向上下左右四个方向流,但是只能向高度小于等于当前高度的方向流,返回同时被两种海水经过的点的坐标。

1、分别处理每个海洋,从海洋边缘(每个海洋两条边)开始,一步步的搜索,即从连接海洋的地方开始搜索,哪些节点的高度大于等于自身(反过来就是可以从那里流到自己),如果是,就标记为true,就这样不断搜索下去。最后所有标记为true的位置就是可以流到对应的海洋。 
2、找这两个矩阵,同为true的输出,就是都能流到两个海洋的位置

dfs解决:

class Solution { //22ms
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;
        int m = matrix.length;
        int n = matrix[0].length;
        boolean[][] pacific = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];
        //递归遍历每个位置
        for (int i = 0;i < m;i ++){//由第一列和最后一列开始递归遍历
            dfs(matrix,i,0,pacific,Integer.MIN_VALUE);
            dfs(matrix,i,n - 1,atlantic,Integer.MIN_VALUE);
        }
        for (int j = 0;j < n;j ++){//由第一行和最后一行开始递归遍历
            dfs(matrix,0,j,pacific,Integer.MIN_VALUE);
            dfs(matrix,m - 1,j,atlantic,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;
    }
    public void dfs(int[][] matrix,int i,int j,boolean[][] isvisited,int pre){
        int m = matrix.length;
        int n = matrix[0].length;
        if (i < 0 || i >= m || j < 0 || j >= n || isvisited[i][j] || matrix[i][j] < pre){
            return;
        }
        isvisited[i][j] = true;
        dfs(matrix,i + 1,j,isvisited,matrix[i][j]);
        dfs(matrix,i - 1,j,isvisited,matrix[i][j]);
        dfs(matrix,i,j + 1,isvisited,matrix[i][j]);
        dfs(matrix,i,j - 1,isvisited,matrix[i][j]);
    }
}

② bfs解决:

class Solution { //31ms
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;
        int m = matrix.length;
        int n = matrix[0].length;
        boolean[][] pacific = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];
        Queue<int[]> q1 = new LinkedList<>();
        Queue<int[]> q2 = new LinkedList<>();
        for (int i = 0;i < m;i ++){
            q1.offer(new int[]{i,0});
            q2.offer(new int[]{i,n - 1});
            pacific[i][0] = true;
            atlantic[i][n - 1] = true;
        }
        for (int j = 0;j < n;j ++){
            q1.offer(new int[]{0,j});
            q2.offer(new int[]{m - 1,j});
            pacific[0][j] = true;
            atlantic[m - 1][j] = true;
        }
        bfs(matrix,pacific,q1);
        bfs(matrix,atlantic,q2);
        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;
    }
    public void bfs(int[][] matrix,boolean[][] isvisited,Queue<int[]> q){
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dirs = {{0,-1},{-1,0},{0,1},{1,0}};
        while(! q.isEmpty()){
            int[] tmp = q.poll();
            for (int[] dir : dirs){
                int x = tmp[0] + dir[0];
                int y = tmp[1] + dir[1];
                if (x < 0 || x >= m || y < 0 || y >= n || isvisited[x][y] || matrix[x][y] < matrix[tmp[0]][tmp[1]]){
                    continue;
                }
                isvisited[x][y] = true;
                q.offer(new int[]{x,y});
            }
        }
    }
}

转载于:https://my.oschina.net/liyurong/blog/1601476

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值