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.

class Solution {
public:
    vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
        int m=grid.size();
        int n=grid[0].size();
        vector<int> result(hits.size(),0);
        set<pair<int,int>> remain;//剩余不掉落的砖块,必须和i=0处的砖块连通,其他砖块都是hits之后掉落的
        //让它减一而不是变为零,是因为可能hit了多次,只有还原最开始的那一次hit之后才能将这个砖块恢复
        for (int i=0;i<hits.size();i++) grid[hits[i][0]][hits[i][1]]--;
        for (int j=0;j<n;j++) if(grid[0][j]==1) findRemain(grid,0,j,remain);
        //从最后一次hit开始,逐步恢复砖块,并且找到因为hit当前砖块而掉落的砖块,将其加入remain中
        for (int k=hits.size()-1;k>=0;k--) 
        {
            int pre_remain=remain.size();
            int i=hits[k][0];
            int j=hits[k][1];
            grid[i][j]++;
            if(grid[i][j]!=1) continue; //说明hit了多次,还没有到最开始的那个hit,所以不能恢复
            else if((i>0&&remain.count({i-1,j}))||(i<m-1&&remain.count({i+1,j}))
            ||(j>0&&remain.count({i,j-1}))||(j<n-1&&remain.count({i,j+1}))||i==0)
            { //当一个砖块周围的所有位置都是0,说明在hit它的时候它已经掉落了,那么就不应该搜索恢复它周围的砖块,因为它们不是因为hit它而掉落的
                findRemain(grid,i,j,remain);
                result[k]=remain.size()-pre_remain-1; //恢复了一个砖块,不算在掉落砖块数内
            }
        }
        return result;
    }
    
    void findRemain(vector<vector<int>>& grid, int i, int j, set<pair<int,int>>& remain)
    {
        int m=grid.size();
        int n=grid[0].size();
        if(remain.count({i,j})) return;
        if(grid[i][j]!=1) return; //由于可能存在负数,所以不能用grid[i][j]==0
        remain.insert({i,j});
        if(i>0) findRemain(grid,i-1,j,remain);
        if(i<m-1) findRemain(grid,i+1,j,remain);
        if(j>0) findRemain(grid,i,j-1,remain);
        if(j<n-1) findRemain(grid,i,j+1,remain);
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值