★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ncqklzux-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.git
The world is modeled as a 2-D array of cells, where 0
represents uninfected cells, and 1
represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary.github
Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited. Each day, you can install walls around only one region -- the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night. There will never be a tie.api
Can you save the day? If so, what is the number of walls required? If not, and the world becomes fully infected, return the number of walls used. 微信
Example 1:app
Input: grid = [[0,1,0,0,0,0,0,1], [0,1,0,0,0,0,0,1], [0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,0]] Output: 10 Explanation: There are 2 contaminated regions. On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is: [[0,1,0,0,0,0,1,1], [0,1,0,0,0,0,1,1], [0,0,0,0,0,0,1,1], [0,0,0,0,0,0,0,1]] On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.
Example 2:less
Input: grid = [[1,1,1], [1,0,1], [1,1,1]] Output: 4 Explanation: Even though there is only one cell saved, there are 4 walls built. Notice that walls are only built on the shared boundary of two different cells.
Example 3:ui
Input: grid = [[1,1,1,0,0,0,0,0,0], [1,0,1,0,1,1,1,1,1], [1,1,1,0,0,0,0,0,0]] Output: 13 Explanation: The region on the left only builds two new walls.
Note:spa
grid
will each be in the range [1, 50]
.grid[i][j]
will be either 0
or 1
.病毒擴散得很快,如今你的任務是儘量地經過安裝防火牆來隔離病毒。code
假設世界由二維矩陣組成,0
表示該區域未感染病毒,而 1
表示該區域已感染病毒。能夠在任意 2 個四方向相鄰單元之間的共享邊界上安裝一個防火牆(而且只有一個防火牆)。
天天晚上,病毒會從被感染區域向相鄰未感染區域擴散,除非被防火牆隔離。現因爲資源有限,天天你只能安裝一系列防火牆來隔離其中一個被病毒感染的區域(一個區域或連續的一片區域),且該感染區域對未感染區域的威脅最大且保證惟一。
你須要努力使得最後有部分區域不被病毒感染,若是能夠成功,那麼返回須要使用的防火牆個數; 若是沒法實現,則返回在世界被病毒所有感染時已安裝的防火牆個數。
示例 1:
輸入: grid = [[0,1,0,0,0,0,0,1], [0,1,0,0,0,0,0,1], [0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,0]] 輸出: 10 說明: 一共有兩塊被病毒感染的區域: 從左往右第一塊須要 5 個防火牆,同時若該區域不隔離,晚上將感染 5 個未感染區域(即被威脅的未感染區域個數爲 5); 第二塊須要 4 個防火牆,同理被威脅的未感染區域個數是 4。所以,第一天先隔離左邊的感染區域,通過一晚後,病毒傳播後世界以下: [[0,1,0,0,0,0,1,1], [0,1,0,0,0,0,1,1], [0,0,0,0,0,0,1,1], [0,0,0,0,0,0,0,1]] 第二題,只剩下一塊未隔離的被感染的連續區域,此時須要安裝 5 個防火牆,且安裝完畢後病毒隔離任務完成。
示例 2:
輸入: grid = [[1,1,1], [1,0,1], [1,1,1]] 輸出: 4 說明: 此時只須要安裝 4 面防火牆,就有一小區域能夠倖存,不被病毒感染。 注意不須要在世界邊界創建防火牆。
示例 3:
輸入: grid = [[1,1,1,0,0,0,0,0,0], [1,0,1,0,1,1,1,1,1], [1,1,1,0,0,0,0,0,0]] 輸出: 13 說明: 在隔離右邊感染區域後,隔離左邊病毒區域只須要 2 個防火牆了。
說明:
grid
的行數和列數範圍是 [1, 50]。grid[i][j]
只包含 0
或 1
。1 class Solution { 2 func containVirus(_ grid: [[Int]]) -> Int { 3 var grid = grid 4 var res:Int = 0 5 var m:Int = grid.count 6 var n:Int = grid[0].count 7 var dirs:[[Int]] = [[-1,0],[0,1],[1,0],[0,-1]] 8 while (true ) 9 { 10 var visited:Set<Int> = Set<Int>() 11 var all:[[[Int]]] = [[[Int]]]() 12 for i in 0..<m 13 { 14 for j in 0..<n 15 { 16 if grid[i][j] == 1 && !visited.contains(i * n + j) 17 { 18 var q:[Int] = [i * n + j] 19 var virus:[Int] = [i * n + j] 20 var walls:[Int] = [Int]() 21 visited.insert(i * n + j) 22 while(!q.isEmpty) 23 { 24 var t:Int = q.removeFirst() 25 for dir in dirs 26 { 27 var x:Int = (t / n) + dir[0] 28 var y:Int = (t % n) + dir[1] 29 if x < 0 || x >= m || y < 0 || y >= n || visited.contains(x * n + y) 30 { 31 continue 32 } 33 if grid[x][y] == -1 34 { 35 continue 36 } 37 else if grid[x][y] == 0 38 { 39 walls.append(x * n + y) 40 } 41 else if grid[x][y] == 1 42 { 43 visited.insert(x * n + y) 44 virus.append(x * n + y) 45 q.append(x * n + y) 46 } 47 } 48 } 49 var s:Set<Int> = Set(walls) 50 var cells:[Int] = [s.count] 51 all.append([cells ,walls, virus]) 52 } 53 } 54 } 55 if all.isEmpty {break} 56 all.sort(by:{(a:[[Int]],b:[[Int]]) -> Bool in 57 return a[0][0] > b[0][0]}) 58 for i in 0..<all.count 59 { 60 if i == 0 61 { 62 var virus:[Int] = all[0][2] 63 for idx in virus 64 { 65 grid[idx / n][idx % n] = -1 66 } 67 res += all[0][1].count 68 } 69 else 70 { 71 var wall:[Int] = all[i][1] 72 for idx in wall 73 { 74 grid[idx / n][idx % n] = 1 75 } 76 } 77 } 78 } 79 return res 80 } 81 }