Board Score
Asked at Airbnb
Reported by the problem's creator, not verified
A board game is scored from a grid of tiles. Each tile is a two-character string: a capital letter for its area type, followed by a digit for the number of crowns on it, for example "G2" is a type G tile with 2 crowns.
A region is a maximal group of tiles of the same area type that are connected through shared sides (up, down, left, right; touching only at a corner does not connect). The crown digits do not matter for connecting, only the letter. The score of a region is the number of tiles in it multiplied by the total crowns on those tiles. A region with no crowns scores 0. The score of the board is the sum of the scores of all its regions.
Write board_score(board), where board is a rectangular list of rows of tile strings, and return the score of the board.
Examples
Example 1
Input: board =
[["G1","G2","W0","W1","S1"],["G2","G3","W0","W1","S1"],["S2","S3","S1","G1","S1"],["G1","G2","W0","W1","S1"],["G1","G2","W0","W1","S1"]]Output:
116Explanation: The regions are: top-left
G(4 cells, 8 crowns, 32), topW(4 cells, 2 crowns, 8), theScolumn on the right edge (5 cells, 5 crowns, 25), the rowS2 S3 S1(3 cells, 6 crowns, 18), the loneG1(1 cell, 1 crown, 1), bottom-leftG(4 cells, 6 crowns, 24) and bottomW(4 cells, 2 crowns, 8). The total is32 + 8 + 25 + 18 + 1 + 24 + 8 = 116.