TopCoder problem "DominoesGame" used in SRM 251 (Division I Level Two)



Problem Statement

    Dominoes is a game played with rectangular tiles. Each tile (domino) is split in half and contains two numbers between 0 and 6 inclusive. A tile that has two identical numbers is called a double. Each tile is unique, meaning that there are no two tiles that have the same numbers. Here is an example of what a single domino looks like:
 .-------.
 | 4 | 3 |
 '-------'
The first tile can be played without any restriction. Future tiles must be played in a single row, but can be played on either end of the row, so that they connect with the existing tiles. This process is continued until no more tiles can be legally played. A tile connects with another tile if they have a common number, in which case the tiles are connected such that the common numbers face each other. The exception to this is that if a tile is a double then it is placed crosswise in the line of tiles. For example, a valid row of dominoes might look like this:
                            .---.
 .-------..-------..-------.| 6 |.-------.
 | 4 | 3 || 3 | 0 || 0 | 6 || - || 6 | 1 |
 '-------''-------''-------'| 6 |'-------'
                            '---'
Notice that when two dominos touch, the touching numbers are the same.

The 'value' of a row of dominoes is equal to the sum of the numbers on its outer edges. Hence, the value of the above configuration is 4+1=5. If a double is on one of the ends of the row, both of the numbers on it are counted towards the value. So, without the rightmost tile, the above configuration would have a value of 4+(6+6)=16. When there is just one tile, the value is simply the sum of the two numbers on that tile. If, after playing a tile, the value of a row is divisible by 5, then the value is added to the overall score.

Given a String[] of tiles that a player possesses return the maximum score that the player can accumulate by placing some or all of his tiles in some order. Each tile will be in the format <x>:<y> where x and y are the two numbers on the tile.

 

Definition

    
Class:DominoesGame
Method:largestTotal
Parameters:String[]
Returns:int
Method signature:int largestTotal(String[] tiles)
(be sure your method is public)
    
 

Constraints

-tiles will contain between 1 and 10 elements inclusive.
-Each element in tiles will be formatted as <x>:<y> where x and y are the two numbers on the tile. x and y will be integers with no leading zeroes between 0 and 6 inclusive.
-tiles will have no repeated elements. Note that <x>:<y> and <y>:<x> represent the same tile.
 

Examples

0)
    
{"0:0","0:5","5:5"}
Returns: 30
The best way to do this is to first place the "5:5" tile, scoring 10 points. Then, add the "5:0" tile scoring 10 more points. Finally, add the "0:0" tile, scoring another 10 points.
1)
    
{"5:3","3:4","4:4","4:6","6:6","6:5","5:5"}
Returns: 70
The only way to make 70 is to place the tiles in the following order: "4:6", "4:4", "6:6", "3:4", "5:3", "6:5", "5:5".
2)
    
{"0:0","0:1","0:2","0:3","0:4","0:5","0:6","1:1","1:2","1:3"}
Returns: 20
3)
    
{"0:0","1:1","2:2","3:3","4:4","5:5","6:6"}
Returns: 10
Here we cannot use all the tiles, but we can still make 10 points.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4573

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=7226&pm=4573

Writer:

dimkadimon

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Brute Force, Recursion, Simulation