Problem Statement
In the word game Boggle, 16 cubes are placed in a container that is shaken. The scattered cubes fall into a 4x4 grid structure. The top faces of each cube compose a playable area where players have to find words constructed from adjacent letters. For instance, the playable area might look like either of these:
ABCD XYZI EFGH ABIJ IJKL RSLN MNOP POOM
In the example to the right, we could form the word "BARS" by starting at the B and travelling left-down-right. Note that we could not, however, create the word SOON, as it would require a diagonal move from the O to the N.
In our version of the game, the path must travel horizontally or vertically at each step without reusing any cubes. For instance, the word COAT might be found in many different arrangements (other arrangments are also possible):
COAT COA CO CO
T TA AT
In our version of the game, a player scores points for any word from a list of acceptable words. The score for each word is the square of its length. Thus, a word with three letters scores 9, four letters scores 16, etc. If the same word can be formed in more than one way from the grid, each distinct path is counted. Also, words which are a part of a longer word may be counted. Examples:
TEXX CATX REEX XXSX
In the first example, the word TREE may be formed in two different ways: starting with the T, we may go down-right-up, or we may go down-right-right. Thus, we score points for the four-letter word tree twice (total 32 points). In the second example, we score points for CAT and CATS, assuming both were in the list of acceptable words. Thus, we score 9 + 16 = 25 points.
Each cube has six different letters on its sides (no letter will appear more than once on the same cube). The arrangement of the cubes in the grid, and which face is upward (in play) is perfectly random. You are given a String[] cubes, with exactly 16 elements, each element of which is six letters long, indicating the letters that appear on each of the cubes. You are also given a String[] dict, each element of which is one of the allowed words.
You are to return a double indicating your expected score, assuming you play perfectly and find all possible paths for all acceptable words that can be found in the 4x4 grid.
Definition
- Class:
- WordBoggle
- Method:
- expectedScore
- Parameters:
- String[], String[]
- Returns:
- double
- Method signature:
- double expectedScore(String[] cubes, String[] dict)
- (be sure your method is public)
Notes
- Return values must be accurate to 1e-9, relative or absolute.
Constraints
- cubes will have exactly 16 elements.
- Each element of cubes will have exactly 6 distinct uppercase letters ('A'-'Z') characters.
- dict will have between 1 and 50 elements, inclusive.
- Each element of dict will contain between 1 and 8 uppercase letters ('A'-'Z') characters, inclusive.
- Each element of dict will be distinct.
Examples
{"ABCDEI", "ABCDEJ", "ABCDEK", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF"}{"I"}Returns: 0.16666666666666666
Here, there is only one target word, and only one cube that can be used. There is a 1 in 6 chance of that cube having the 'I' showing, which would score 1 point. Thus, the expected score is 1/6.{"ABCDEI", "ABCDEJ", "ABCDEK", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF"}{"ABCDABCD","EFEFEFEF"}Returns: 0.1048729505519629
Some more complicated words that can be formed many different ways using the available cubes. Note also that each 8 letter word scores 64 points.{"ABCDEI", "ABCDEJ", "ABCDEK", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF", "ABCDEF"}{"A"}Returns: 2.6666666666666665
For each of the 16 cubes, there is a 1 in 6 chance of the letter 'A' showing. Each one scores one point, so our expected score is 16/6.