You are given a triangular board of cells, where each cell is an equilateral triangle. Cells are arranged as shown in the picture below.
Two cells are adjacent if they have a common edge. Thus, in the example above, cells B, C and D are adjacent to cell A. There are three types of cells: "colored", "remove adjacent" and "remove row". A colored cell has a color between 0 and 9, inclusive. The other two types of cells have no color.
You will choose one cell and remove it. You will then possibly remove more cells recursively according to the following rules:
-
If you removed a colored cell, you must recursively remove all adjacent colored cells with the same color.
-
If you removed a "remove adjacent" cell, you must recursively remove all adjacent cells, regardless of their types.
- If you removed a "remove row" cell, you must recursively remove all the cells in that same row, regardless of their types.
"Recursively removing a cell" means that you must remove this cell, check its type and apply the corresponding rule to it to recursively remove more cells (see examples 3-6 for further clarification). You can remove a cell only once, i.e., you can not remove a cell if it was removed before.
You are given a String[] board containing the rows of the board from top to bottom. Each element of board represents a single row of cells. Element i (0-based) of board contains exactly 2*i+1 characters, each representing a single cell. 'A' represents "remove adjacent" cells, 'R' represents "remove row" cells, and the digits '0'-'9' represent colored cells with colors 0 through 9, respectively.
Your total score is the number of colored cells you removed (so "remove adjacent" and "remove row" cells do not count toward your score). Return the maximal score you can get on the given board.
|