0417. Pacific Atlantic Water Flow (M)

Pacific Atlantic Water Flow (M)

题目

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

题意

给定一个二维数组,数组第一排上方和第一列左侧是太平洋,最后一排下方和最后一列右侧是大西洋,每个元素代表该处水位的高度,每个位置的水只能向低处流动。计算所有既能到达太平洋又能到达大西洋的水的位置。

思路

关键是要标记出所有能到达太平洋/大西洋的坐标。以太平洋为例,将第一行及第一列所有位置标记为可到达,利用BFS或DFS将剩余所有可到达的位置标记出来。最后筛选出既可到达太平洋又可到达大西洋的坐标。


代码实现

Java

class Solution {
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        if (matrix.length == 0) return new ArrayList<>();
        
        List<List<Integer>> ans = new ArrayList<>();
        int m = matrix.length, n = matrix[0].length;
        boolean[][] toPacific = new boolean[m][n];
        boolean[][] toAtlantic = new boolean[m][n];

        judge(matrix, toPacific, 0, 0);
        judge(matrix, toAtlantic, m - 1, n - 1);

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (toPacific[i][j] && toAtlantic[i][j]) {
                    ans.add(Arrays.asList(i, j));
                }
            }
        }

        return ans;
    }

    private void judge(int[][] matrix, boolean[][] toOcean, int row, int col) {
        Queue<int[]> q = new LinkedList<>();
        int m = matrix.length, n = matrix[0].length;
        int[] shiftX = {-1, 1, 0, 0}, shiftY = {0, 0, -1, 1};

        for (int i = 0; i < n; i++) {
            toOcean[row][i] = true;
            q.offer(new int[]{row, i});
        }
        for (int i = 0; i < m; i++) {
            toOcean[i][col] = true;
            if (i != row) q.offer(new int[]{i, col});
        }

        while (!q.isEmpty()) {
            int[] coord = q.poll();
            int x = coord[0], y = coord[1];
            for (int i = 0; i < 4; i++) {
                int nextX = x + shiftX[i], nextY = y + shiftY[i];
                if (isValid(m, n, nextX, nextY) && matrix[x][y] <= matrix[nextX][nextY] &&!toOcean[nextX][nextY]) {
                    toOcean[nextX][nextY] = true;
                    q.offer(new int[]{nextX, nextY});
                }
            }
        }
    }

    private boolean isValid(int m, int n, int i, int j) {
        return i >= 0 && i < m && j >= 0 && j < n;
    }
}
posted @ 2021-03-25 18:08  墨云黑  阅读(28)  评论(0编辑  收藏  举报