★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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:微信
咱们有一组包含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 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 }