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))
  • 相关阅读:
    centos“You don't have permission to access /index.html on this server.”
    使用yum来安装或卸载CentOS图形界面包
    PHP上IIS上显示服务器应用程序不可用
    Windows 2003 远程桌面连接数超过最大连接数终极解决方案
    perl清理电脑上重复的文件
    wordpress在IIS6上首页访问不了
    WordPress首页出现循环重定向解决办法四则(转载)
    windows server 2003 如何在远程链接时更改用户密码
    Ubuntu下安装php5gd库,支持jpg、png、gif等格式图片处理
    wordpress设置用163邮箱账号发送邮件
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/11525353.html
Copyright © 2011-2022 走看看