You are given an m x n binary grid, where each 1 represents a brick and 0 represents an empty space. A brick is stable if:
It is directly connected to the top of the grid, or
At least one other brick in its four adjacent cells is stable.
You are also given an array hits, which is a sequence of erasures we want to apply. Each time we want to erase the brick at the location hits[i] = (rowi, coli). The brick on that location (if it exists) will disappear. Some other bricks may no longer be stable because of that erasure and will fall. Once a brick falls, it is immediately erased from the grid (i.e., it does not land on other stable bricks).
Return an array result, where each result[i] is the number of bricks that will fall after the ith erasure is applied.
Note that an erasure may refer to a location with no brick, and if it does, no bricks drop.
Example 1:
Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
Output: [2]
Explanation: Starting with the grid:
[[1,0,0,0],
[1,1,1,0]]
We erase the underlined brick at (1,0), resulting in the grid:
[[1,0,0,0],
[0,1,1,0]]
The two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:
[[1,0,0,0],
[0,0,0,0]]
Hence the result is [2].
Example 2:
Input: grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
Output: [0,0]
Explanation: Starting with the grid:
[[1,0,0,0],
[1,1,0,0]]
We erase the underlined brick at (1,1), resulting in the grid:
[[1,0,0,0],
[1,0,0,0]]
All remaining bricks are still stable, so no bricks fall. The grid remains the same:
[[1,0,0,0],
[1,0,0,0]]
Next, we erase the underlined brick at (1,0), resulting in the grid:
[[1,0,0,0],
[0,0,0,0]]
Once again, all remaining bricks are still stable, so no bricks fall.
Hence the result is [0,0].
Constraints:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 200
- grid[i][j] is 0 or 1.
- 1 <= hits.length <= 4 * 104
- hits[i].length == 2
- 0 <= xi <= m - 1
- 0 <= yi <= n - 1
- All (xi, yi) are unique.
代码最后已经写成流水账了, 没有什么参考价值了, 大家就不要看了。说一下思路, 我们首先假设所有要打的砖块都打完了, 但是该掉落的砖块都还保持在自己的位置上, 这时候我们就可以把剩余的相邻砖块组成 components, 然后我们再反向的将已经打掉的砖块再放回到 grid 中来, 这时候如果放回的砖块可以将那些本身并不相连的 components 连接起来的话, 那我们再检查这些 components 本身是不是与天花板相连, 如果连接起来的 components 里面(包括放回的那个砖块自己), 至少有一个与天花板相连, 那我们打掉这块砖的动作所能引起的掉落砖块数量就是这些连接起来的 components 里面不与天花板相连的那些 components 中的砖块数量。这部分有点绕,但是并不复杂, 自己推演一下就好。
impl Solution {
fn mark_bricks(grid: &mut Vec<Vec<i32>>, r: usize, c: usize, id: usize) -> (i32, bool) {
if grid[r][c] != 1 {
return (0, false);
}
grid[r][c] = id as i32;
let (left_count, left_reach) = if c > 0 { Solution::mark_bricks(grid, r, c - 1, id) } else { (0, false) };
let (right_count, right_reach) = if c < grid[0].len() - 1 { Solution::mark_bricks(grid, r, c + 1, id) } else { (0, false) };
let (up_count, up_reach) = if r > 0 { Solution::mark_bricks(grid, r - 1, c, id) } else { (0, false) };
let (down_count, down_reach) = if r < grid.len() - 1 { Solution::mark_bricks(grid, r + 1, c, id) } else { (0, false) };
return (left_count + right_count + up_count + down_count + 1, r == 0 || left_reach || right_reach || up_reach || down_reach);
}
fn find(components: &Vec<(i32, bool, usize)>, id: usize) -> (i32, bool, usize) {
let c = components[id];
if c.2 == id {
return c;
}
Solution::find(components, c.2)
}
fn union(components: &mut Vec<(i32, bool, usize)>, id1: usize, id2: usize) {
if id1 < 2 || id2 < 2 {
return;
}
let (c1, r1, i1) = Solution::find(components, id1);
let (c2, r2, i2) = Solution::find(components, id2);
if i1 == i2 {
return;
}
components[i2].2 = i1;
components[i1].0 += c2;
if !r1 && !r2 {
return;
}
components[i1].1 = true;
components[i2].1 = true;
}
pub fn hit_bricks(mut grid: Vec<Vec<i32>>, hits: Vec<Vec<i32>>) -> Vec<i32> {
for h in &hits {
if grid[h[0] as usize][h[1] as usize] == 1 {
grid[h[0] as usize][h[1] as usize] = 0;
continue;
}
grid[h[0] as usize][h[1] as usize] = -1;
}
let mut id = 1;
let mut components = vec![(0, false, 0), (0, false, 0)];
for r in 0..grid.len() {
for c in 0..grid[0].len() {
if grid[r][c] == 1 {
id += 1;
let (count, reach) = Solution::mark_bricks(&mut grid, r, c, id);
components.push((count, reach, id as usize));
}
}
}
let mut ans = Vec::new();
for h in hits.into_iter().rev() {
let r = h[0] as usize;
let c = h[1] as usize;
if grid[r][c] == -1 {
ans.push(0);
continue;
}
let id = components.len();
grid[r][c] = id as i32;
components.push((1, r == 0, id));
let li = if c > 0 && grid[r][c - 1] > 1 { grid[r][c - 1] as usize } else { 0 };
let ri = if c < grid[0].len() - 1 && grid[r][c + 1] > 1 { grid[r][c + 1] as usize } else { 0 };
let ui = if r > 0 && grid[r - 1][c] > 1 { grid[r - 1][c] as usize } else { 0 };
let di = if r < grid.len() - 1 && grid[r + 1][c] > 1 { grid[r + 1][c] as usize } else { 0 };
let (lc, lr, lp) = Solution::find(&components, li);
let (rc, rr, rp) = Solution::find(&components, ri);
let (uc, ur, up) = Solution::find(&components, ui);
let (dc, dr, dp) = Solution::find(&components, di);
if r == 0 || lr || rr || ur || dr {
let mut unreached_count = 0;
let mut parents = HashSet::new();
if !lr && !parents.contains(&lp) {
unreached_count += lc;
parents.insert(lp);
}
if !rr && !parents.contains(&rp) {
unreached_count += rc;
parents.insert(rp);
}
if !ur && !parents.contains(&up) {
unreached_count += uc;
parents.insert(up);
}
if !dr && !parents.contains(&dp) {
unreached_count += dc;
parents.insert(dp);
}
ans.push(unreached_count);
} else {
ans.push(0);
}
Solution::union(&mut components, id, lp);
Solution::union(&mut components, id, rp);
Solution::union(&mut components, id, up);
Solution::union(&mut components, id, dp);
}
ans.into_iter().rev().collect()
}
}