Problem Statement
MATCHING GAMES OF DOMINOES
This section is identical in all three problems - you may skip it if you are
familiar with matching games of dominoes.
Games of dominoes use tiles as playing pieces. Each tile has two numbers on it
- one at each end. The range of the numbers on the tiles depends on the size of
the dominoes set: double-six sets use numbers zero through six, double-nine
sets use numbers zero through nine, and so on. Each set has exactly one tile
for each pair of numbers in the range - a double-six set contains 28 tiles 0-0
through 6-6, double-nine set contains 55 tiles 0-0 through 9-9, and so on.
Tiles are symmetric - there is one tile for each pair of numbers, regardless of
the order of numbers (for example, the 3-5 tile is the same as the 5-3 tile).
Matching games of dominoes require building trains of tiles. A train is a group
of tiles arranged linearly in such a way that numbers facing each other on the
adjacent tiles match. For example, 1-2 2-3 3-0 0-0 0-6 is a train, while 1-2
3-4 is not a train.
PROBLEM STATEMENT
Implement a class GameCheck with a method lastValid that analyzes moves in a
matching game of dominoes, using one double-nine set. Your method should return
the index of the last valid move.
DEFINITION
Class Name: GameCheck
Method Name: lastValid
Parameters: int[]
Returns: int
Method signature (be sure your method is public): int lastValid (int[] moves);
moves contains values in the range 0 to 99, inclusive, representing moves in a
matching game of dominoes. For a given number, the tens digit is on one side of
a tile and the ones digit is on the other. Symmetric tiles are identical, so
values 16 and 61 refer to the same tile. TopCoder will check that moves
contains between 1 and 50 elements, and that all the values are within the
allowed range of 0 to 99, inclusive.
Rules for checking validity of a move:
- The first move is always valid.
- All subsequent moves are valid if it is possible to build a valid train from
all tiles index zero through current, playing them in the order of increasing
indexes.
- If a duplicate tile is encountered in the input, it is considered invalid.
- Under no circumstances can you move a previously played tile.
NOTES
- Symmetric tiles are identical; therefore fitting a tile to a train may
involve flipping the tile, and trying both sides of a train.
EXAMPLES
For clarity, all single-digit numbers in the examples are shown with a leading
zero.
- if moves = {01,02,31,12}, lastValid should return 3, because you can use all
4 tiles to build a valid train:
- Start the train with 0-1
- Flip 0-2 and attach it to the front of the train to form 2-0 0-1
- Flip 3-1 and attach it to the end of the train to form 2-0 0-1 1-3
- Attach 1-2 in front of the train to form 1-2 2-0 0-1 1-3.
- if moves = {01,10}, lastValid should return 0. Even though the move 1-0 fits
both ends of the 0-1 train, it is still considered invalid, since it uses a
previously played tile.
- if moves = {01,02,11,22,43,21}, lastValid should return 3, because 4-3 does
not fit on either side of the 1-1 1-0 0-2 2-2 train.
- if moves = {12,23,13}, lastValid should return 2, because both 1-2 2-3 3-1
and 3-1 1-2 2-3 are valid (the 13 tile could be attached to either end of the
existing train), and tile index 2 is the last tile that can be played.Definition
- Class:
- GameCheck
- Method:
- lastValid
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int lastValid(int[] param0)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.