• 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))
  • 相关阅读:
    markdown自动生成侧边栏TOC /目录
    jquery和javascript的区别
    Jquery中AJAX参数详细(1)-转
    jQuery.ajax介绍
    人人开源分模块,非原生html报错,很难查找问题所在,有vue语法
    《SSH网上商城》-视频目录--代码可以跑起来
    《第16项目:国家税务协同平台项目》-视频目录
    项目:《ssh框架综合项目开发视频》-视频目录和第六天的EasyUI简单讲解
    项目:《JavaWeb图书管理系统视频》--代码修复还可以运行起来
    Maven项目在更新过程停止,再更新无效-->解决
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/11525353.html
Copyright © 2020-2023  润新知