[Swift]LeetCode803. 打磚塊 | Bricks Falling When Hit

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-dguwlpos-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

We have a grid of 1s and 0s; the 1s in a cell represent bricks.  A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.git

We will do some erasures sequentially. Each time we want to do the erasure at the location (i, j), the brick (if it exists) on that location will disappear, and then some other bricks may drop because of that erasure.github

Return an array representing the number of bricks that will drop after each erasure in sequence.數組

Example 1:
Input: 
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
Output: [2]
Explanation: 
If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.
Example 2:
Input: 
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
Output: [0,0]
Explanation: 
When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. So each erasure will cause no bricks dropping.  Note that the erased brick (1, 0) will not be counted as a dropped brick.

Note:微信

  • The number of rows and columns in the grid will be in the range [1, 200].
  • The number of erasures will not exceed the area of the grid.
  • It is guaranteed that each erasure will be different from any other erasure, and located inside the grid.
  • An erasure may refer to a location with no brick - if it does, no bricks drop.

咱們有一組包含1和0的網格;其中1表示磚塊。 當且僅當一塊磚直接鏈接到網格的頂部,或者它至少有一塊相鄰(4 個方向之一)磚塊不會掉落時,它纔不會落下。app

咱們會依次消除一些磚塊。每當咱們消除 (i, j) 位置時, 對應位置的磚塊(若存在)會消失,而後其餘的磚塊可能由於這個消除而落下。ide

返回一個數組表示每次消除操做對應落下的磚塊數目。spa

示例 1:
輸入:
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
輸出: [2]
解釋: 
若是咱們消除(1, 0)位置的磚塊, 在(1, 1) 和(1, 2) 的磚塊會落下。因此咱們應該返回2。
示例 2:
輸入:
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
輸出:[0,0]
解釋:
當咱們消除(1, 0)的磚塊時,(1, 1)的磚塊已經因爲上一步消除而消失了。因此每次消除操做不會形成磚塊落下。注意(1, 0)磚塊不會記做落下的磚塊。

注意:code

  • 網格的行數和列數的範圍是[1, 200]。
  • 消除的數字不會超過網格的區域。
  • 能夠保證每次的消除都不相同,而且位於網格的內部。
  • 一個消除的位置可能沒有磚塊,若是這樣的話,就不會有磚塊落下。

Runtime: 588 ms
Memory Usage: 21.9 MB
 1 class Solution {
 2     func hitBricks(_ grid: [[Int]], _ hits: [[Int]]) -> [Int] {
 3         var grid = grid
 4         var hits = hits
 5         var m:Int = grid.count
 6         var n:Int = grid[0].count
 7         var k:Int =  hits.count
 8         var res:[Int] = [Int](repeating:0,count:k)
 9         var noDrop:Set<Int> = Set<Int>()
10         for i in 0..<k
11         {
12             grid[hits[i][0]][hits[i][1]] -= 1
13         }
14         for i in 0..<n
15         {
16            if grid[0][i] == 1
17             {
18                 check(&grid, 0, i, &noDrop)
19             }
20         }
21         for i in stride(from:k - 1,through:0,by:-1)
22         {
23             var oldSize:Int = noDrop.count
24             var x:Int = hits[i][0]
25             var y:Int = hits[i][1]
26             grid[x][y] += 1
27             if grid[x][y] != 1 {continue}
28             if (x - 1 >= 0 && noDrop.contains((x - 1) * n + y))
29                 || (x + 1 < m && noDrop.contains((x + 1) * n + y))
30                 || (y - 1 >= 0 && noDrop.contains(x * n + y - 1))
31                 || (y + 1 < n && noDrop.contains(x * n + y + 1))
32                 || x == 0
33             {
34                 check(&grid, x, y, &noDrop)
35                 res[i] = noDrop.count - oldSize - 1
36             }
37         }
38         return res
39     }
40     
41     func check(_ grid:inout [[Int]],_ i:Int,_ j:Int,_ noDrop:inout Set<Int>)
42     {
43         var m:Int = grid.count
44         var n:Int = grid[0].count
45         if i < 0 || i >= m || j < 0 || j >= n || grid[i][j] != 1 || noDrop.contains(i * n + j)
46         {
47             return        
48         }
49         noDrop.insert(i * n + j)
50         check(&grid, i - 1, j, &noDrop)
51         check(&grid, i + 1, j, &noDrop)
52         check(&grid, i, j - 1, &noDrop)
53         check(&grid, i, j + 1, &noDrop)
54     }
55 }
相關文章
相關標籤/搜索