KatInterview

Chain-Reaction Tile Clearing on a Grid

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

Last updated

Simulate a grid game where runs of three or more equal tiles vanish together, tiles fall down, and the process repeats until the grid is stable.

Chain-Reaction Tile Clearing

You are given a 2D grid board of integers with m rows and n columns. Each positive integer is a tile type, and 0 marks an empty cell. Row 0 is the top of the grid and row m - 1 is the bottom.

The grid is processed with the following rules, repeated until nothing more can be cleared:

  1. Detect. A cell is selected if it belongs to a horizontal or vertical run of three or more consecutive cells that all hold the same non-zero value. A cell can be part of both a horizontal and a vertical run, so shapes such as L, T and + are possible. Empty cells (0) are never selected.
  2. Clear. All selected cells are emptied (set to 0) at the same moment. Detection must be completed for the entire grid before any cell is emptied.
  3. Drop. After clearing, tiles fall straight down within their column until each one rests on the bottom edge or on another tile. The relative vertical order of the tiles in a column does not change. Empty cells end up at the top of the column.
  4. Repeat. Falling tiles may form new runs, so go back to step 1. Stop when step 1 selects no cells.

Return the final grid.

Task

Implement the function that takes board and returns the stable grid after all chain reactions have finished.

Constraints

  • 1 <= m, n <= 50
  • board[i][j] is an integer with 0 <= board[i][j] <= 2000
  • 0 denotes an empty cell; any other value is a tile
  • Runs are only horizontal or vertical; diagonals do not count
  • A grid with fewer than three rows or columns can still be processed; it just cannot form runs in that direction
  • The result must have the same dimensions as the input

Examples

Example 1

Input: clear_tile_chains([[110, 5, 112, 113, 114], [210, 211, 5, 213, 214], [310, 311, 3, 313, 314], [410, 411, 412, 5, 414], [5, 1, 512, 3, 3], [610, 4, 1, 613, 614], [710, 1, 2, 713, 714], [810, 1, 2, 1, 1], [1, 1, 2, 2, 2], [4, 1, 4, 4, 1014]])

Output: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [110, 0, 0, 0, 114], [210, 0, 0, 0, 214], [310, 0, 0, 113, 314], [410, 0, 0, 213, 414], [610, 211, 112, 313, 614], [710, 311, 412, 613, 714], [810, 411, 512, 713, 1014]]

Example 2

Input: clear_tile_chains([[1, 1, 1], [2, 2, 2], [3, 4, 5]])

Output: [[0, 0, 0], [0, 0, 0], [3, 4, 5]]

Example 3

Input: clear_tile_chains([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Hints

Hint 1

Compute everything that should disappear in one round before changing the grid. Use a separate marker structure so a horizontal clear cannot hide a vertical run.

Hint 2

Checking every window of three consecutive cells, horizontally and vertically, is enough: longer runs are just overlapping windows. Remember to ignore windows of zeros.

Hint 3

For gravity, walk each column from the bottom up with a write pointer that receives each non-zero value, then fill whatever remains above it with zeros. Loop the whole round until no cell is cleared.