• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode803.打砖块|BricksFallingWhenHit

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10548672.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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.

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 个方向之一)砖块不会掉落时,它才不会落下。

我们会依次消除一些砖块。每当我们消除 (i, j) 位置时, 对应位置的砖块(若存在)会消失,然后其他的砖块可能因为这个消除而落下。

返回一个数组表示每次消除操作对应落下的砖块数目。

示例 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)砖块不会记作落下的砖块。

注意:

  • 网格的行数和列数的范围是[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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
3.Swift数组|字典|集合发布时间:2022-07-13
下一篇:
[Swift]LeetCode546.移除盒子|RemoveBoxes发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap