[Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:为敢(WeiGanTechnologies)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10332885.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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).
给定一个 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]] (上图中带括号的单元).
320ms
1 class Solution { 2 func pacificAtlantic(_ matrix: [[Int]]) -> [[Int]] { 3 var matrix = matrix 4 if matrix.isEmpty || matrix[0].isEmpty {return []} 5 var res:[[Int]] = [[Int]]() 6 var m:Int = matrix.count 7 var n:Int = matrix[0].count 8 var pacific:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:n),count:m) 9 var atlantic:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:n),count:m) 10 for i in 0..<m 11 { 12 dfs(&matrix, &pacific, Int.min, i, 0) 13 dfs(&matrix, &atlantic, Int.min, i, n - 1) 14 } 15 for i in 0..<n 16 { 17 dfs(&matrix, &pacific, Int.min, 0, i) 18 dfs(&matrix, &atlantic, Int.min, m - 1, i) 19 } 20 21 for i in 0..<m 22 { 23 for j in 0..<n 24 { 25 if pacific[i][j] && atlantic[i][j] 26 { 27 res.append([i,j]) 28 } 29 } 30 } 31 return res 32 } 33 34 func dfs(_ matrix:inout [[Int]],_ visited:inout [[Bool]],_ pre:Int,_ i:Int,_ j:Int) 35 { 36 var m:Int = matrix.count 37 var n:Int = matrix[0].count 38 if i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || matrix[i][j] < pre 39 { 40 return 41 } 42 visited[i][j] = true 43 dfs(&matrix, &visited, matrix[i][j], i + 1, j) 44 dfs(&matrix, &visited, matrix[i][j], i - 1, j) 45 dfs(&matrix, &visited, matrix[i][j], i, j + 1) 46 dfs(&matrix, &visited, matrix[i][j], i, j - 1) 47 } 48 }
3572ms
1 class Solution { 2 var reachable: [[(Bool, Bool)]] = [] 3 4 func pacificAtlantic(_ matrix: [[Int]]) -> [[Int]] { 5 guard !matrix.isEmpty && !matrix[0].isEmpty else { return [] } 6 let n = matrix.count 7 let m = matrix[0].count 8 reachable = Array(repeating: Array(repeating: (false, false), count: m), count: n) 9 var visited: [[Bool]] = Array(repeating: Array(repeating: false, count: m), count: n) 10 11 for i in 0 ..< n { 12 for j in 0 ..< m { 13 dfs(matrix, i, j, &visited) 14 } 15 } 16 17 var res: [[Int]] = [] 18 19 for i in 0 ..< n { 20 for j in 0 ..< m { 21 if reachable[i][j].0 && reachable[i][j].1 { 22 res.append([i, j]) 23 } 24 } 25 } 26 return res 27 } 28 29 private func dfs(_ matrix: [[Int]], _ x: Int, _ y: Int, _ visited: inout [[Bool]]) -> (Bool, Bool) { 30 let n = matrix.count 31 let m = matrix[0].count 32 if reachable[x][y].0 && reachable[x][y].1 { 33 return (true, true) 34 } 35 visited[x][y] = true 36 let dirs: [(Int, Int)] = [(-1, 0), (0, -1), (1, 0), (0, 1)] 37 for dir in dirs { 38 let newX = x + dir.0 39 let newY = y + dir.1 40 if newX >= 0 && newX < n && newY >= 0 && newY < m && matrix[newX][newY] > matrix[x][y] { 41 reachable[x][y].0 = reachable[x][y].0 || false 42 reachable[x][y].1 = reachable[x][y].1 || false 43 } else if newX < 0 || newX >= n || newY < 0 || newY >= m { 44 reachable[x][y].0 = reachable[x][y].0 || (newX < 0 || newY < 0) 45 reachable[x][y].1 = reachable[x][y].1 || (newX >= n || newY >= m) 46 } else if !visited[newX][newY] { 47 let res = dfs(matrix, newX, newY, &visited) 48 reachable[x][y].0 = reachable[x][y].0 || res.0 49 reachable[x][y].1 = reachable[x][y].1 || res.1 50 } 51 } 52 visited[x][y] = false 53 return reachable[x][y] 54 } 55 }