leetcode803. Bricks Falling When Hit(python)

803. Bricks Falling When Hit(python)


原题地址:https://leetcode.com/problems/bricks-falling-when-hit/

摘自leetcode 用户:cthbst

题目

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 located inside the grid.

  • An erasure may refer to a location with no brick - if it does, no bricks drop.

python代码

class Solution:
    def hitBricks(self, grid, hits):
        """
        :type grid: List[List[int]]
        :type hits: List[List[int]]
        :rtype: List[int]
        """
        grid2 = [[[] for j in i] for i in grid]
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                for i2, j2 in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
                    if not (i2 < 0 or i2 >= len(grid) or j2 < 0 or j2 >= len(grid[0])):
                        grid2[i][j].append((i2, j2))

        grid3 = [[[] for j in i] for i in grid]

        for j in range(len(grid[0])):
            queue = [(0, j)]
            seen = set(queue)
            while queue:
                i, j = queue.pop(0)
                seen.add((i, j))
                if grid[i][j] == 1:
                    for child in grid2[i][j]:
                        i2, j2 = child
                        if i2 == 0:
                            continue
                        if child not in seen and (i, j) not in grid3[i2][j2]:
                            queue.append(child)
                            grid3[child[0]][child[1]].append((i, j))

        ans = []
        for i, j in hits:
            grid[i][j] = 0
            queue = [(i, j)]
            seen = set(queue)
            res = 0
            while queue:
                i2, j2 = queue.pop(0)
                seen.add((i2, j2))
                for child in grid2[i2][j2]:
                    if child in seen: continue
                    if child[0] == 0: continue
                    i3, j3 = child
                    if grid[i3][j3] == 0: continue
                    if (i2, j2) in grid3[i3][j3]:
                        grid3[child[0]][child[1]].remove((i2, j2))

                    x, y = i3, j3
                    while len(grid3[x][y]) == 1:
                        i4, j4 = grid3[x][y][0]
                        if (x, y) in grid3[i4][j4]:
                            grid3[i4][j4].remove((x, y))
                            if len(grid3[i4][j4]) == 1:
                                x, y = i4, j4
                            else:
                                break
                        else:
                            break

                    if not grid3[child[0]][child[1]]:
                        grid[i3][j3] = 0
                        queue.append((i3, j3))
                        res += 1
            ans.append(res)
        return ans

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值