Crush and Fall
board is a match-three puzzle given as a list of rows: 0 is an empty cell and a positive number is a candy of that colour. The board starts settled: in every column, all empty cells are above all candies. One round works like this. First, find every candy that belongs to a horizontal or vertical run of three or more neighbouring candies of the same colour; all runs are found before anything is removed, and a candy may be part of a horizontal and a vertical run at once. Then remove all of those candies at the same time. Finally, let every remaining candy fall straight down its column until no empty cell is below it. Repeat rounds until a round finds no run, and return the final board as a new board, leaving board itself unchanged.
Example 1
The bottom row is a run of three 1s and is removed. The candies above fall one row, and no new run appears.
Example 2
The three 2s in column 0 are removed and the 1 above them falls to the bottom row, where it completes a row of three 1s. That row goes in the second round, and the candies above it fall down.
Constraints
1 <= rows, cols <= 100 <= board[r][c] <= 5- in every column, all the
0cells are above all the candies
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Four in a RowMedium
- Life-Like GenerationsMedium
- Minutes to BurnMedium
- Paths Around RocksMedium
- Ramp DropMedium
- Settle the SandpileMedium
- Slide and MergeMedium
- Snake GameMedium