SameGame
TCO '03 Round 2 · 2003-10-07 · by lbackstrom
Problem Statement
The input will be formatted as a
For example, {"RBB","RRR","RBB"} represents:
RBB
RRR
RBB
Where 'R' and 'B' are the only two colors present.
One possible sequence of moves would be to first remove the five 'R's, which are all connected and form a group. This would give us 10 points (5*4/2) and make the board look as follows: (where '-' represents empty space):
-BB
---
-BB
Now the pieces fall down and shift to the left, giving:
---
BB-
BB-
If we then remove the group of 4 B's, we get an additional 6 points. This
removes all of the blocks so we multiply the total (16) by 4, and our final
score is 64.
If the board is small enough, we can simply use brute force to find the optimal sequence of groups to remove to obtain the highest score. However, as the number of blocks grows, brute force quickly becomes infeasible. Thus, in order to write a program that plays well, we must use some intelligent technique to remove pieces - albeit a sub-optimal one. One important observation to make in trying to come up with a good strategy is that the number of points grows non-linearly, and we get more points per block if we remove larger groups. This suggests that we should save one color for last, and remove smaller groups first. Your task is to implement the following strategy:
You will be given a
Constraints
- board will contain between 1 and 50 elements, inclusive.
- Each element of board will contain between 1 and 50 characters, inclusive.
- Each element of board will contain the same number of characters.
- Each character of each element of board will be an uppercase letter ('A'-'Z').
- order will contain between 1 and 26 uppercase letters, inclusive.
- No character will occur more than once in order.
- Each character in board will also be in order.
{"RBB",
"RRR",
"RBB"}
"RB"
Returns: 64
This is the example from the problem statement.
{"ABCD",
"ABCD",
"ABCD",
"ABCD"}
"ABCD"
Returns: 96
Here, we end up removing one column at a time, starting with the 'A's, and ending with the 'D's. After we remove each column, the remaining ones shift to the left. Each column gives us 4*3/2 points, for a total of 24 points. Since we can remove all of the blocks, we get a final score of 96.
{"ABCD"}
"ABCD"
Returns: 0
{"ABCD",
"AABC",
"AAAB",
"ABBA"}
"ACBD"
Returns: 32
{"AAZZ"}
"AZ"
Returns: 8
{"ACCAA",
"ABAAA",
"ABBBA",
"AAACD"}
"EABCD"
Returns: 28
First, we remove the 'L' shaped group of six 'A's, drop the blocks over empty space, and then shift left to get the following ('-' denotes empty space): --AA- CCAA- BABA- BBCD- Next, we remove the group of 5 'A's and end up with: ----- CC--- BAB-- BBCD- Finally, we remove the 3 'B's and get this board, which contains no groups: ----- ----- -CB-- CACD-
Submissions are judged against all 79 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SameGame with a public method int getScore(vector<string> board, string order) · 79 test cases · 2 s / 256 MB per case