1. 程式人生 > >[Swift Weekly Contest 109]LeetCode934. 最短的橋 | Shortest Bridge

[Swift Weekly Contest 109]LeetCode934. 最短的橋 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)

Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.

Return the smallest number of 0s 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. 1 <= A.length = A[0].length <= 100
  2. A[i][j] == 0 or A[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. 1 <= A.length = A[0].length <= 100
  2. 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 }