[Swift]LeetCode803. 打砖块 | Bricks Falling When Hit

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 }

 

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10548672.html
0条评论
添加一条新回复