803. Bricks Falling When Hit

本文介绍了一种模拟砖块在网格中掉落的算法。通过在一系列hit操作后,计算每一步hit导致的砖块掉落数量。算法首先移除所有hit位置的砖块,然后标记与顶部相连的所有砖块,再逐步恢复hit位置并计算新增掉落的砖块数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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.

Problem URL


Solution

给一个二维数组,1表示砖,只要与顶部联通,砖就不会掉落,在一系列的hit之后,找到所有掉下来的砖。

Code
class Solution {
    private static final int TOP = 2;
    private static final int BRICK = 1;
    private static final int EMPTY = 0;
    private static final int[][] DIRS = {{1, 0}, {-1, 0}, {0, 1},{0, -1}};
    public int[] hitBricks(int[][] grid, int[][] hits) {
        int[] res = new int[hits.length];
        //remove all hits and then add them back
        for (int[] hit : hits){
            grid[hit[0]][hit[1]]--;
        }
        
        //assign 2 to all grids connected to top;
        for (int i = 0; i < grid[0].length; i++){
            dfs(0, i, grid);
        }
        
        //add back removed hitted bricks
        for (int i = hits.length - 1; i >= 0; i--){
            int x = hits[i][0], y = hits[i][1];
            grid[x][y]++;
            if (grid[x][y] == BRICK && isConnected(x, y, grid)){
                res[i] = dfs(x, y, grid) - 1;
            }
        }
        return res;
    }
    
    private int dfs(int i, int j, int[][] grid){
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != BRICK){
            return 0;
        }
        grid[i][j] = 2;
        return dfs(i + 1, j, grid) +
        dfs(i - 1, j, grid) +
        dfs(i, j + 1, grid) +
        dfs(i, j - 1, grid) + 1;
    }
    
    private boolean isConnected(int i, int j, int[][] grid){
        if (i == 0){
            return true;
        }
        for (int[] dir : DIRS){
            int x = i + dir[0], y = j + dir[1];
            if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && grid[x][y] == TOP){
                return true;
            }
        }
        return false;
    }
}

Time Complexity: O()
Space Complexity: O()


Review
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值