原题地址:https://leetcode-cn.com/problems/pacific-atlantic-water-flow/submissions/
题目描述:
给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。
规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。
请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。
提示:
输出坐标的顺序不重要
m 和 n 都小于150
示例:
给定下面的 5x5 矩阵:
太平洋 ~ ~ ~ ~ ~
~ 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 *
* * * * * 大西洋
返回:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (上图中带括号的单元).
解题方案:
有点像动态规划,又有点深度遍历的意思:
class Solution:
def dfs(self, m1: List[List[int]], m2: List[List[int]], v: int, x: int, y: int) -> List[List[int]]:
m = len(m1)
n = len(m1[0])
if x >= 0 and x < m and y >= 0 and y < n and m2[x][y] == 0 and m1[x][y] >= v:
m2[x][y] = 1
m2 = self.dfs(m1, m2, m1[x][y], x + 1, y)
m2 = self.dfs(m1, m2, m1[x][y], x - 1, y)
m2 = self.dfs(m1, m2, m1[x][y], x, y + 1)
m2 = self.dfs(m1, m2, m1[x][y], x, y - 1)
return m2
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
res = []
m = len(matrix)
if m == 0:
return res
n = len(matrix[0])
pacific = [[0] * n for _ in range(m)]
atlantic = [[0] * n for _ in range(m)]
for i in range(m):
pacific = self.dfs(matrix, pacific, -1, i, 0)
atlantic = self.dfs(matrix, atlantic, -1, i, n - 1)
for i in range(n):
pacific = self.dfs(matrix, pacific, -1, 0, i);
atlantic = self.dfs(matrix, atlantic, -1, m - 1, i);
for i in range(m):
for j in range(n):
if pacific[i][j] and atlantic[i][j]:
res.append([i, j])
return res
学习一下最快的方法:
class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if matrix == []:
return []
from collections import deque
row, col = len(matrix), len(matrix[0])
visited1 = [[False]*col for _ in range(row)]
trace = deque()
for c in range(0, col):
visited1[0][c] = True
trace.append((0, c))
for r in range(1, row):
visited1[r][0] = True
trace.append((r, 0))
while(len(trace)):
i, j = trace.popleft()
if i-1>=0 and matrix[i-1][j] >= matrix[i][j] and visited1[i-1][j] == False:
visited1[i-1][j] = True
trace.append((i-1, j))
if i+1= matrix[i][j] and visited1[i+1][j] == False:
visited1[i+1][j] = True
trace.append((i+1, j))
if j-1>=0 and matrix[i][j-1] >= matrix[i][j] and visited1[i][j-1] == False:
visited1[i][j-1] = True
trace.append((i, j-1))
if j+1 = matrix[i][j] and visited1[i][j+1] == False:
visited1[i][j+1] = True
trace.append((i, j+1))
visited2 = [[False]*col for _ in range(row)]
trace = deque()
for c in range(0, col):
visited2[row-1][c] = True
trace.append((row-1, c))
for r in range(0, row-1):
visited2[r][col-1] = True
trace.append((r, col-1))
while(len(trace)):
i, j = trace.popleft()
if i-1>=0 and matrix[i-1][j] >= matrix[i][j] and visited2[i-1][j] == False:
visited2[i-1][j] = True
trace.append((i-1, j))
if i+1= matrix[i][j] and visited2[i+1][j] == False:
visited2[i+1][j] = True
trace.append((i+1, j))
if j-1>=0 and matrix[i][j-1] >= matrix[i][j] and visited2[i][j-1] == False:
visited2[i][j-1] = True
trace.append((i, j-1))
if j+1 = matrix[i][j] and visited2[i][j+1] == False:
visited2[i][j+1] = True
trace.append((i, j+1))
# print(visited1)
# print(visited2)
res = list()
for r in range(row):
for c in range(col):
if visited1[r][c] and visited2[r][c]:
res.append([r, c])
return res
没有递归函数,速度会快很多