zoukankan      html  css  js  c++  java
  • Pacific Atlantic Water Flow 解答

    Question

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

    Solution

    这道题的解题思路与Surrounded Regions类似。

    已知在边缘上的点一定能到达Pacific或Atlantic,我们从边缘上的点开始遍历(BFS/DFS),得到所有能到达Pacific或Atlantic的点,最后取交集就是答案。

    DFS

     1 class Solution:
     2     def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
     3         if not matrix or not matrix[0]:
     4             return []
     5         m, n = len(matrix), len(matrix[0])
     6         result = []
     7         self.directions = [[0, -1], [0, 1], [-1, 0], [1, 0]]
     8         pacific = [[False for i in range(n)] for j in range(m)]
     9         atlantic = [[False for i in range(n)] for j in range(m)]
    10         # DFS
    11         for i in range(m):
    12             self.dfs(matrix, pacific, matrix[i][0], i , 0)
    13             self.dfs(matrix, atlantic, matrix[i][n - 1], i, n - 1)
    14         for i in range(n):
    15             self.dfs(matrix, pacific, matrix[0][i], 0, i)
    16             self.dfs(matrix, atlantic, matrix[m - 1][i], m - 1, i)
    17             
    18         for i in range(m):
    19             for j in range(n):
    20                 if pacific[i][j] and atlantic[i][j]:
    21                     result.append([i, j])
    22         return result
    23         
    24     def dfs(self, matrix: List[List[int]], visited: List[List[bool]], pre: int, i: int, j: int) -> None:
    25         m, n = len(matrix), len(matrix[0])
    26         if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] or matrix[i][j] < pre:
    27             return
    28         visited[i][j] = True
    29         for direction in self.directions:
    30             self.dfs(matrix, visited, matrix[i][j], i + direction[0], j + direction[1])

    BFS

     1 from collections import deque
     2 class Solution:
     3     def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
     4         if not matrix or not matrix[0]:
     5             return []
     6         m, n = len(matrix), len(matrix[0])
     7         result = []
     8         self.directions = [[0, -1], [0, 1], [-1, 0], [1, 0]]
     9         pacific = [[False for i in range(n)] for j in range(m)]
    10         atlantic = [[False for i in range(n)] for j in range(m)]
    11         # BFS
    12         for i in range(m):
    13             self.bfs(matrix, pacific, i , 0)
    14             self.bfs(matrix, atlantic, i, n - 1)
    15         for i in range(n):
    16             self.bfs(matrix, pacific, 0, i)
    17             self.bfs(matrix, atlantic, m - 1, i)
    18             
    19         for i in range(m):
    20             for j in range(n):
    21                 if pacific[i][j] and atlantic[i][j]:
    22                     result.append([i, j])
    23         return result
    24         
    25     def bfs(self, matrix: List[List[int]], visited: List[List[bool]], i: int, j: int) -> None:
    26         # Avoid duplicate calculation
    27         if visited[i][j]:
    28             return
    29         m, n = len(matrix), len(matrix[0])
    30         visited[i][j] = True
    31         level = deque()
    32         level.append((i, j))
    33         while level:
    34             L = len(level)
    35             for _ in range(L):
    36                 curr_i, curr_j = level.popleft()
    37                 for direction in self.directions:
    38                     next_i = curr_i + direction[0]
    39                     next_j = curr_j + direction[1]
    40                     if 0 <= next_i < m and 0 <= next_j < n and not visited[next_i][next_j] and matrix[next_i][next_j] >= matrix[curr_i][curr_j]:
    41                         visited[next_i][next_j] = True
    42                         level.append((next_i, next_j))
  • 相关阅读:
    HashMap 统计一个字符串中每个单词出现的次数
    iOS .a静态库的制作及使用
    iOS framework静态库中使用xib和图片资源详解
    iOS 工程套子工程,主工程和framework工程或.a library静态库工程联调
    iOS 最新framework和.a静态库制作及使用全解(含工程套工程,多工程联调)
    iOS9新特性 3DTouch 开发教程全解(含源码)
    iOS GCD NSOperation NSThread等多线程各种举例详解
    Mac Beyond Compare 永久试用
    cocoapods 常见问题
    iOS 常用工具库LFKit功能介绍
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/11525353.html
Copyright © 2011-2022 走看看