KatInterview

Cascading Tile Crush on a Grid

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingsimulationmatrixtwo-pointers

Last updated

Simulate a tile-crush game: mark all runs of 3+ equal tiles, clear them, drop tiles with gravity, and repeat until stable, counting removed tiles.

Cascading Tile Crush on a Grid

You are given a rectangular grid of tiles. Each tile is a positive integer that identifies its kind. The value 0 means the cell is empty. Simulate the following crush-and-drop process until the grid is stable, then report the final grid and how many tiles were removed in total.

One round

  1. Mark. Look at every maximal horizontal run and every maximal vertical run of equal, non-zero tiles. If a run has length 3 or more, every tile in it is marked. A tile in both a qualifying horizontal run and a qualifying vertical run (an L, T or plus shape) is marked once. Diagonal neighbours never form runs. Empty cells (0) never match each other.
  2. Clear. Only after all marking is finished, set every marked cell to 0. All removals in a round happen simultaneously, so clearing one run must not change which other runs qualify in the same round.
  3. Drop. In each column independently, the surviving non-zero tiles fall straight down, keeping their relative top-to-bottom order. The column's empty cells end up at the top.
  4. Repeat. Start a new round on the resulting grid. Stop when a round marks no tiles.

The total count is the number of distinct cells cleared across all rounds. A cell that is in two runs in the same round counts once.

Task

Write a function that takes the grid and returns a two-element list [final_grid, total_removed], where final_grid is the stable grid (a list of rows) and total_removed is an integer.

Details to get right

  • Do not modify the input grid. Return a new grid.
  • Gravity is applied only after a round that removed something. If the first round marks nothing, return the grid exactly as given, with a count of 0, even if it already contains 0 cells that are not at the top of their columns.
  • If the grid has no rows, return [[], 0].
  • If rows are empty (zero columns), return a copy of the grid and 0.

Constraints

  • 0 <= m, n <= 50, where m is the number of rows and n the number of columns. All rows have the same length.
  • Each cell is an integer in the range 0 to 10^4, where 0 means empty.
  • The process always ends, since each productive round removes at least three tiles.

Examples

Example 1

Input: crush_board([[1, 1, 1], [2, 3, 2], [2, 2, 2]])

Output: [[[0, 0, 0], [0, 0, 0], [2, 3, 2]], 6]

Example 2

Input: crush_board([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

Output: [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], 9]

Example 3

Input: crush_board([[1, 2, 1, 2], [2, 1, 2, 1], [1, 2, 1, 2], [2, 1, 2, 1]])

Output: [[[1, 2, 1, 2], [2, 1, 2, 1], [1, 2, 1, 2], [2, 1, 2, 1]], 0]

Hints

Hint 1

Never clear cells while you are still scanning. Record matches in a separate boolean grid first, otherwise an L or T shape can lose part of its run.

Hint 2

To find runs, walk along each row and each column, extending a pointer while the value stays the same. If the run length is at least 3, mark the whole run, then jump past it. Count marked cells, not run lengths, so overlaps are counted once.

Hint 3

For gravity, process each column from the bottom up with a write pointer: copy each non-zero value to the write position and move the pointer up, then fill what is left above with zeros. Loop until a round marks nothing.