zoukankan      html  css  js  c++  java
  • Leetcode: 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).

    这题考点在于需要设置两个visited数组

    Two Queue and add all the Pacific border to one queue; Atlantic border to another queue.

    Keep a visited matrix for each queue. In the end, add the cell visited by two queue to the result.
    BFS: Water flood from ocean to the cell. Since water can only flow from high/equal cell to low cell, add the neighboor cell with height larger or equal to current cell to the queue and mark as visited.(逆流而上)

    Solution 1: 我自己的DFS (beat 89%)

     1 public class Solution {
     2     int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
     3     public List<int[]> pacificAtlantic(int[][] matrix) {
     4         List<int[]> res = new ArrayList<int[]>();
     5         if (matrix==null || matrix.length==0 || matrix[0].length==0) return res;
     6         int n = matrix.length, m = matrix[0].length;
     7         boolean[][] pVisited = new boolean[n][m];
     8         boolean[][] aVisited = new boolean[n][m];
     9         for (int i=0; i<n; i++) {
    10             //pacific
    11             dfs(matrix, i, 0, pVisited);
    12             //atlatic
    13             dfs(matrix, i, m-1, aVisited);
    14         }
    15         
    16         for (int j=0; j<m; j++) {
    17             //pacific
    18             dfs(matrix, 0, j, pVisited);
    19             //atlatic
    20             dfs(matrix, n-1, j, aVisited);
    21         }
    22         
    23         for (int i=0; i<n; i++) {
    24             for (int j=0; j<m; j++) {
    25                 if (pVisited[i][j] && aVisited[i][j])
    26                     res.add(new int[]{i, j});
    27             }
    28         }
    29         return res;
    30     }
    31     
    32     public void dfs(int[][] matrix, int i, int j, boolean[][] visited) {
    33         int n = matrix.length, m = matrix[0].length;
    34         visited[i][j] = true;
    35         for (int[] dir : directions) {
    36             int row = dir[0] + i;
    37             int col = dir[1] + j;
    38             if (row>=0 && row<n && col>=0 && col<m && !visited[row][col] && matrix[i][j]<=matrix[row][col]) 
    39                 dfs(matrix, row, col, visited);
    40         }
    41     }
    42 }

    Solution 2: BFS, refer to https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean/2

     1 public class Solution {
     2     int[][]dir = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
     3     public List<int[]> pacificAtlantic(int[][] matrix) {
     4         List<int[]> res = new LinkedList<>();
     5         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
     6             return res;
     7         }
     8         int n = matrix.length, m = matrix[0].length;
     9         //One visited map for each ocean
    10         boolean[][] pacific = new boolean[n][m];
    11         boolean[][] atlantic = new boolean[n][m];
    12         Queue<int[]> pQueue = new LinkedList<>();
    13         Queue<int[]> aQueue = new LinkedList<>();
    14         for(int i=0; i<n; i++){ //Vertical border
    15             pQueue.offer(new int[]{i, 0});
    16             aQueue.offer(new int[]{i, m-1});
    17             pacific[i][0] = true;
    18             atlantic[i][m-1] = true;
    19         }
    20         for(int i=0; i<m; i++){ //Horizontal border
    21             pQueue.offer(new int[]{0, i});
    22             aQueue.offer(new int[]{n-1, i});
    23             pacific[0][i] = true;
    24             atlantic[n-1][i] = true;
    25         }
    26         bfs(matrix, pQueue, pacific);
    27         bfs(matrix, aQueue, atlantic);
    28         for(int i=0; i<n; i++){
    29             for(int j=0; j<m; j++){
    30                 if(pacific[i][j] && atlantic[i][j])
    31                     res.add(new int[]{i,j});
    32             }
    33         }
    34         return res;
    35     }
    36     public void bfs(int[][]matrix, Queue<int[]> queue, boolean[][]visited){
    37         int n = matrix.length, m = matrix[0].length;
    38         while(!queue.isEmpty()){
    39             int[] cur = queue.poll();
    40             for(int[] d:dir){
    41                 int x = cur[0]+d[0];
    42                 int y = cur[1]+d[1];
    43                 if(x<0 || x>=n || y<0 || y>=m || visited[x][y] || matrix[x][y] > matrix[cur[0]][cur[1]]){
    44                     continue;
    45                 }
    46                 visited[x][y] = true;
    47                 queue.offer(new int[]{x, y});
    48             } 
    49         }
    50     }
    51 }
  • 相关阅读:
    数组添加元素到特定位置
    jquery $().each,$.each的区别
    JSON字符串 与 JSON对象 互转
    js 获取 url 参数
    js 获取随机数 Math.random()
    js中的|| 与 &&
    js立即执行函数
    css 蒙层
    css 多行文本的溢出显示省略号(移动端)
    移动端利用-webkit-box水平垂直居中(旧弹性盒)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6128362.html
Copyright © 2011-2022 走看看