Bricks Falling When Hit
Master this topic with zero to advance depth.
Bricks Falling When Hit
You are given an binary grid where 1 represents a brick. A brick will not fall if it is connected to the top row (row index 0) or another brick that will not fall. When a brick is 'hit', it turns into 0. Return the number of bricks that fall after each hit.
Reverse Union-Find Strategy
- Remove all hit bricks first.
- Build the initial graph for the remaining bricks.
- Add hit bricks back in reverse order.
- The number of new bricks connected to the top row (excluding the re-added brick itself) is the number of falling bricks for that hit.
Examples
Level II: BFS (Exhaustive Simulation)
Intuition
This approach directly simulates the process for each hit. After removing a brick, we perform a broad connectivity check (using BFS/DFS) to see which bricks are still connected to the top row. Any brick that was previously connected but is no longer is counted as 'fallen'.
Level III: Reverse DSU (Connectivity Recovery)
Intuition
Tracking 'falling' bricks is hard. Tracking 'recovered' bricks by adding them back in reverse is much easier. Use a dummy node to represent the 'ceiling' (connected to all row 0 bricks).
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.