803. 打砖块

803. 打砖块

有一个 m x n 的二元网格,其中 1 表示砖块,0 表示空白。砖块 稳定(不会掉落)的前提是:

一块砖直接连接到网格的顶部,或者
至少有一块相邻(4 个方向之一)砖块 稳定 不会掉落时
给你一个数组 hits ,这是需要依次消除砖块的位置。每当消除 hits[i] = (rowi, coli) 位置上的砖块时,对应位置的砖块(若存在)会消失,然后其他的砖块可能因为这一消除操作而掉落。一旦砖块掉落,它会立即从网格中消失(即,它不会落在其他稳定的砖块上)。

返回一个数组 result ,其中 result[i] 表示第 i 次消除操作对应掉落的砖块数目。

注意,消除可能指向是没有砖块的空白位置,如果发生这种情况,则没有砖块掉落。

思路

  • 可以想到,只要连通着顶部的砖头的石头,都是稳定砖头,也就是一个连通分量。
  • 打掉一个砖头后,相当于将一个连通分量拆成两部分。
  • 因此可以逆序使用并查集,首先讲待打碎的砖头打碎,然后创建并查集,然后逆序将砖头补上,补上后有多少砖头可以变成稳定,就证明打碎时有多少砖头掉落。
  • 创建一个特殊节点,表示屋顶

代码

class Solution {
private:
    vector<int> father;
    //用于计算节点有多少个砖头
    vector<int> count;
    int m, n;
    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, -1, 0, 1};
public:
    int find(int x) {
        if(father[x] == x) {
            return x;
        }
        return father[x] = find(father[x]);
    }
    void join(int x, int y) {
        int fx = find(father[x]);
        int fy = find(father[y]);
        if(fx == fy) {
            return;
        }
        father[fx] = fy;
        count[fy] += count[fx];
    }
    void init(int size) {
        father.resize(size);
        count.resize(size);
        for(int i = 0; i < size; i++) {
            father[i] = i;
            count[i] = 1;
        }
    }
    int getCount(int x) {
        int fx = find(x);
        return count[fx];
    }
    int getIndex(int x, int y) {
        return x * n + y;
    }
    bool inplace(int x, int y) {
        return x >= 0 && x < m && y >= 0 && y < n;
    }
    vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
        m = grid.size();
        n = grid[0].size();
        int size = m * n;
        init(size + 1);

		//击碎砖头
        vector<vector<int>> status = grid;
        for(auto& hit : hits) {
            status[hit[0]][hit[1]] = 0;
        }

		//连通屋顶和第一层节点
        for(int j = 0; j < n; j++) {
            if(status[0][j] == 1) {
                join(j, size);
            }
        }

		//遍历剩余层节点
        for(int i = 1; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(status[i][j] == 1) {
                	//计算上面是否有砖头可以连通
                    if(status[i - 1][j] == 1) {
                        join(getIndex(i, j), getIndex(i - 1, j));
                    }
                    //左边
                    if(j > 0 && status[i][j - 1] == 1) {
                        join(getIndex(i, j), getIndex(i, j - 1));
                    }
                }
            }
        }

		//逆序补砖头
        int len = hits.size();
        vector<int> ans(len, 0);
        for(int i = len - 1; i >= 0; i--) {
            int x = hits[i][0];
            int y = hits[i][1];

			//击碎点没有砖头,跳过该情况
            if(grid[x][y] == 0) {
                continue;
            }

			//计算连通屋顶的砖头
            int org = getCount(size);

			//如果击碎点是第一层,连通
            if(x == 0) {
                join(y, size);
            }

			//上下左右寻找是否有砖头,连通起来
            for(int k = 0; k < 4; k++) {
                int next_x = x + dx[k];
                int next_y = y + dy[k];

                if(inplace(next_x, next_y) && status[next_x][next_y] == 1) {
                    join(getIndex(x, y), getIndex(next_x, next_y));
                }
            }

			//计算连通后连接屋顶数量
            int now = getCount(size);

            ans[i] = max(0, now - org - 1);
			
			//补上砖头
            status[x][y] = 1;
        }
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值