In a given 2D binary array A
, there are two islands. (An island is a 4-directionally connected group of 1
s not connected to any other 1s.)
Now, we may change 0
s to 1
s so as to connect the two islands together to form 1 island.
Return the smallest number of 0
s that must be flipped. (It is guaranteed that the answer is at least 1.)
Example 1:
Input: [[0,1],[1,0]] Output: 1
Example 2:
Input: [[0,1,0],[0,0,0],[0,0,1]] Output: 2
Example 3:
Input: [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] Output: 1
Note:
1 <= A.length = A[0].length <= 100
A[i][j] == 0
orA[i][j] == 1
在给定的二维二进制数组
A
中,存在两座岛。(岛是由四面相连的 1
形成的一个最大组。)
现在,我们可以将 0
变为 1
,以使两座岛连接起来,变成一座岛。
返回必须翻转的 0
的最小数目。(可以保证答案至少是 1。)
示例 1:
输入:[[0,1],[1,0]] 输出:1
示例 2:
输入:[[0,1,0],[0,0,0],[0,0,1]] 输出:2
示例 3:
输入:[[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] 输出:1
提示:
1 <= A.length = A[0].length <= 100
A[i][j] == 0
或A[i][j] == 1
648ms
1 class Solution { 2 func shortestBridge(_ A: [[Int]]) -> Int { 3 var n:Int = A.count, m = A[0].count 4 var can:[[Bool]] = [[Bool]](repeating: [Bool](repeating: false, count: m), count: n) 5 var dr:[Int] = [1, 0, -1, 0 ] 6 var dc:[Int] = [0, 1, 0, -1 ] 7 var d:[[Int]] = [[Int]](repeating: [Int](repeating: 99999999, count: m), count: n) 8 9 var gq:Queue<[Int]> = Queue<[Int]>() 10 inner: 11 for i in 0..<n 12 { 13 for j in 0..<m 14 { 15 if A[i][j] == 1 16 { 17 var q:Queue<[Int]> = Queue<[Int]>() 18 q.enQueue([i,j]) 19 can[i][j] = true 20 while(!q.isEmpty()) 21 { 22 var cur:[Int] = q.deQueue()! 23 gq.enQueue(cur) 24 var r:Int = cur[0], c:Int = cur[1] 25 d[r][c] = 0 26 for k in 0..<4 27 { 28 var nr:Int = r + dr[k], nc:Int = c + dc[k] 29 if nr >= 0 && nr < n && nc >= 0 && nc < m && A[nr][nc] == 1 && !can[nr][nc] 30 { 31 can[nr][nc] = true 32 q.enQueue([nr, nc]) 33 } 34 } 35 } 36 break inner 37 } 38 } 39 } 40 41 while(!gq.isEmpty()) 42 { 43 var cur:[Int] = gq.deQueue()! 44 var r:Int = cur[0], c:Int = cur[1] 45 for k in 0..<4 46 { 47 var nr:Int = r + dr[k], nc:Int = c + dc[k] 48 if nr >= 0 && nr < n && nc >= 0 && nc < m && d[nr][nc] > d[r][c] + 1 49 { 50 d[nr][nc] = d[r][c] + 1 51 gq.enQueue([nr, nc]) 52 } 53 } 54 } 55 var ret:Int = 9999999 56 for i in 0..<n 57 { 58 for j in 0..<m 59 { 60 if !can[i][j] && A[i][j] == 1 61 { 62 ret = min(ret, d[i][j]) 63 } 64 } 65 } 66 return ret-1 67 } 68 } 69 70 public struct Queue<T> { 71 72 // 泛型数组:用于存储数据元素 73 fileprivate var queue: [T] 74 75 // 返回队列中元素的个数 76 public var count: Int { 77 return queue.count 78 } 79 80 // 构造函数:创建一个空的队列 81 public init() { 82 queue = [T]() 83 } 84 85 //通过既定数组构造队列 86 init(_ arr:[T]){ 87 queue = arr 88 } 89 90 // 如果定义了默认值,则可以在调用函数时省略该参数 91 init(_ elements: T...) { 92 queue = elements 93 } 94 95 // 检查队列是否为空 96 // - returns: 如果队列为空,则返回true,否则返回false 97 public func isEmpty() -> Bool { 98 return queue.isEmpty 99 } 100 101 // 入队列操作:将元素添加到队列的末尾 102 public mutating func enQueue(_ element: T) { 103 queue.append(element) 104 } 105 106 // 出队列操作:删除并返回队列中的第一个元素 107 public mutating func deQueue() -> T? { 108 return queue.removeFirst() 109 } 110 }
原文地址:https://www.cnblogs.com/strengthen/p/9903939.html
时间: 2024-10-08 16:01:30