美文网首页
2020-05-01 417. Pacific Atlantic

2020-05-01 417. Pacific Atlantic

作者: _伦_ | 来源:发表于2020-05-01 12:08 被阅读0次

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

思路就是从边缘开始dfs,最后看哪些节点既能够到达两个海洋。用刀了位运算,因为一个节点有四种状态:被太平洋/大西洋出发的dfs遍历过、能够到达太平洋/大西洋

class Solution {

    public List<List<Integer>> pacificAtlantic(int[][] matrix) {

        List<List<Integer>> res = new LinkedList<>();

        if (matrix == null || matrix.length == 0) return res;

        /* example: 1 1 1 0----- reached by atlantic

                    | | |-- reached by pacific

                    | |

                    | |---- stepped by atlantic

                    |

                    stepped by pacific

        */

        int[][] stepped = new int[matrix.length][matrix[0].length];

        // System.out.println("init stepped: " + Arrays.deepToString(stepped));

        for (int i = 0; i < matrix.length; i++) {

            for (int j = 0; j < matrix[0].length; j++) {

                if (i == 0 || j == 0) {

                    stepped[i][j] |= 0b1010;

                    dfs(matrix, stepped, i, j, 0b1000);

                }

                if (i == matrix.length - 1 || j == matrix[0].length - 1) {

                    stepped[i][j] |= 0b0101;

                    dfs(matrix, stepped, i, j, 0b0100);

                }

            }

        }

        for (int i = 0; i < matrix.length; i++) {

            for (int j = 0; j < matrix[0].length; j++) {

                if ((stepped[i][j] & 0b0011) == 0b0011) {

                    res.add(Arrays.asList(i, j));

                }

            }

        }

        return res;

    }

    void dfs(int[][] matrix, int[][] stepped, int i, int j, int stepMask) {

        if (i - 1 >= 0) dfs(matrix, stepped, i - 1, j, stepMask, i, j);

        if (i + 1 < matrix.length) dfs(matrix, stepped, i + 1, j, stepMask, i, j);

        if (j - 1 >= 0) dfs(matrix, stepped, i, j - 1, stepMask, i, j);

        if (j + 1 < matrix[0].length) dfs(matrix, stepped, i, j + 1, stepMask, i, j);

    }

    void dfs(int[][] matrix, int[][] stepped, int i, int j, int stepMask, int prevX, int prevY) {

        if ((stepped[i][j] & stepMask) != 0) {

            return;

        }

        if (matrix[i][j] >= matrix[prevX][prevY]) {

            stepped[i][j] |= stepMask | stepped[prevX][prevY];

            /*System.out.println(String.format(

                "stepped[%d][%d]: %s, matrix[i][j]: %d, stepped[%d][%d]: %s, matrix[prevX][prevY]: %d",

                        i,  j,  Integer.toBinaryString(stepped[i][j]),

                                                    matrix[i][j],

                                                                prevX,

                                                                    prevY,

                                                                        Integer.toBinaryString(stepped[prevX][prevY]),

                                                                                                  matrix[prevX][prevY]));*/

            dfs(matrix, stepped, i, j, stepMask);

        }

    }

}

相关文章

网友评论

      本文标题:2020-05-01 417. Pacific Atlantic

      本文链接:https://www.haomeiwen.com/subject/vpofghtx.html