[Leetcode] 803. 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.

思路

一道比较难的DFS题目:每次遇到一个hit的时候,我们就将其本身置为空,然后采用DFS的方法检查它的四周。一旦发现它的某一块和第0行相连,那么就可以知道这一连通块的砖不会掉下来;否则这一连通块的砖就要掉下来了,我们就再采用count计算一共有多少块砖会掉下来(在计数的同时我们将grid的对应位置置为0,表示这一连通块的砖已经掉了)。需要注意的是,如果某一连通块的砖不会掉下来,那么我们在随后的检查中有可能还会再次检查到它(因为它在后面的hit中有可能会掉下来),所以我们定义一个全局的id,对于每一轮的falling函数的调用,采用一个唯一的id来标记DFS的边界,而这一id在后面falling的计算中是不起作用的(这一块是算法实现的关键细节,否则程序总有bug)。

我也不知道解释的清楚不清楚,有问题的读者可以在下面留言。

代码

class Solution {
public:
    vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
        row_num = grid.size(), col_num = grid[0].size();
        vector<int> ret;
        for(auto h : hits){
            int r = h[0], c = h[1];
            int removal = 0;
            if(grid[r][c] == 1) {
                grid[r][c] = 0;         // mark this grid empty
                for(int d = 0; d < 4; d++){
                    int x = r + dir[d][0], y = c + dir[d][1];
                    if (!valid(x, y) || grid[x][y] == 0) {
                        continue;
                    }
                    ++id;               // mark each connecting parts with a unique id in this run
                    if(falling(grid, x,y)) {
                        removal += count(grid, x,y);
                    }
                }
            }
            ret.push_back(removal);
        }
        return ret;
    }
private:
    bool falling(vector<vector<int>>& grid, int r, int c) {
        if(!valid(r,c)||grid[r][c] == 0) {
            return true;
        }
        if (visited[r][c] == id) {
            return true;                // visited and belongs to the same part this run
        }
        if (r == 0) {
            return false;               // connecting 1st row
        }
        visited[r][c] = id;
        for (int d = 0; d < 4; ++d) {
            if (!falling(grid, r + dir[d][0], c + dir[d][1])) {
                return false;
            }
        } 
        return true;
    }
    int count(vector<vector<int>>& grid, int r, int c) {
        if (!valid(r,c)||grid[r][c] == 0) {
            return 0;
        }
        int ret = 1;
        grid[r][c] = 0;
        for(int d = 0; d < 4; ++d) {
            ret += count(grid, r + dir[d][0], c + dir[d][1]);
        }
        return ret;
    }
    bool valid(int r, int c) {
        return 0 <= r && r < row_num && 0 <= c && c < col_num;
    }
    vector<vector<int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int visited[201][201];
    int row_num, col_num, id;
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值