Score a tile board by connected terrain regions

0
Sign in

A tile-laying board game ends when every player has filled a rectangular board. You must compute a player's final score.

You are given board, a 2D list of strings with R rows and C columns. Each tile is a 2-character string:

  • The first character is an uppercase letter, the tile's terrain (for example W for water, G for grassland, F for forest).
  • The second character is a digit 0-9, the number of crowns on the tile.

Two tiles are in the same region when they share an edge (up, down, left or right, not diagonal) and have the same terrain letter. Regions are maximal: they include every tile reachable this way. Two regions of the same terrain that do not touch are scored separately.

A region scores (number of tiles in the region) * (total crowns in the region). A region with no crowns scores 0. The board score is the sum of all region scores.

Implement:

def calculate_board_score(board: list[list[str]]) -> int:
    ...

Constraints

  • 0 <= R, C <= 1000; all rows have the same length.
  • Raise ValueError for a malformed tile or ragged rows.
  • Do not modify the input.

Example 1

board = [["W1", "W0", "G0"],
         ["G0", "G2", "G0"],
         ["W0", "F0", "F1"]]
-> 12

The top-left water region has 2 tiles and 1 crown (2 points). The grassland region has 4 tiles and 2 crowns (8 points). The bottom-left water tile touches no other water, so it is its own region with 0 crowns (0 points). The forest region has 2 tiles and 1 crown (2 points).

Example 2

board = [["S1", "W0"],
         ["W0", "S1"]]
-> 2

The two S tiles touch only at a corner, so each is a separate region worth 1 * 1.

WaymoAirbnbCodingMediumLast asked Mar 2026

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.