#AI2301. ACSL Tiles(中级)
ACSL Tiles(中级)
Description
ACSL Tiles is a one-person game played with rectangular tiles. Each tile has a single-digit number between 1 and 9 inclusive at each end. At the start of the game, there are 4 rows, each with a number. The goal of the game is to build rows by placing a tile at the right end of a row whose last number matches a number on the tile. Tiles can be re-oriented; thus, the tiles 34 and 43 are the same tile. If a tile cannot be placed on any row, it is placed in the discard pile. When all tiles have been played or discarded, find the sum of the single-digit numbers on all of the tiles in the discard pile.
At each turn, try the next tile in your hand to see if it can be added to one of the rows, starting with the row after the one where the last tile was placed, rotating back to Row 1 if necessary. Start looking at Row 1 when the game starts. However, if the last tile placed was a double (i.e., both numbers are the same), another tile must be placed on that row before any other match can be considered. If the tile cannot be placed, add it to the discard pile.
Input Format
Input a 4-digit number that gives the initial numbers, from Row 1 to Row 4. It is followed by a string of no more than 50 2-digit integers, each separated by a single space. Each 2-digit number represents the two numbers on each tile.
Output Format
After placing the tiles using the rules above, output the sum of the single-digit numbers on the tiles in the discard pile.
Explanation
Example 1: Input:
5923
56 27 73 34 99 45 32 19 64 57 18
Explanation: The game starts with 4 rows having numbers 5, 9, 2, 3.
- Tile 56 is placed on Row 1 (matches 5). Row 1 ends in 6.
- Tile 27 is placed on Row 3 (matches 2). Row 3 ends in 7.
- Tile 73 is placed on Row 4 as 37 (matches 3). Row 4 ends in 7. (Row 4 is checked first after Row 3).
- Tile 34 cannot be placed on Row 1 (6), 2 (9), 3 (7), or 4 (7). Placed in discard pile.
- Tile 99 is placed on Row 2 (matches 9). It is a double. Row 2 ends in 9. Next tile MUST match Row 2.
- Tile 45 does not match 9. Placed in discard pile.
- Tile 32 does not match 9. Placed in discard pile.
- Tile 19 is placed on Row 2 as 91 (matches 9). Row 2 ends in 1. Double constraint lifted.
- Tile 64 is placed on Row 1 (matches 6). Row 1 ends in 4.
- Tile 57 is placed on Row 3 as 75 (matches 7). Row 3 ends in 5.
- Tile 18 is placed on Row 2 (matches 1). Row 2 ends in 8.
Discard pile: 34, 45, 32. Sum = (3+4) + (4+5) + (3+2) = 7 + 9 + 5 = 21.
5923
56 27 73 34 99 45 32 19 64 57 18
21
4687
81 72 15 89 36 21 13 67 42 93 48 83 45 47 52 94 62
86
1932
94 81 13 43 21 31 89 69 18 28 86 88 29 89 92
11
1957
32 69 87 73 31 88 62
23
1542
24 44 39 32 92 63 47 76 37 78 38
46