Market Board (Large)
Time limit5sMemory limit512 MB
Free dice land on uniform random faces and each cell scores by the longest run of two to four equal faces covering it; find the expected total.
- Level
Medium7 of 10
- Topics
- Probability, Combinatorics
- Solved
- No attempts yet
Problem
While climbing Jirisan you stop at a market day. In one corner of the crowd somebody is running a game.
The game uses a grid board of rows and columns on a table. Every cell of the grid holds one -sided die.
The host fixes some of the dice so that a chosen face shows and leaves the rest loose. The board is then shaken, and every die that is not fixed, called a free die, comes to rest showing one of its faces. Each face shows with the same probability , and the dice are independent of each other.
Once the board stops, every cell is scored. Four directions count: horizontal, vertical, and the two diagonals. A cell is scored like this.
- If some run of four consecutive cells in one direction contains this cell and all four show the same face, the cell scores .
- Otherwise, if some run of three consecutive cells in one direction contains this cell and all three show the same face, the cell scores .
- Otherwise, if some run of two consecutive cells in one direction contains this cell and both show the same face, the cell scores .
- Otherwise the cell scores 0.
The score of the game is the sum of the scores of all cells.
The dice have been fixed and the board is about to be shaken. Compute the expected score of the game.
Input
The first line has the number of test cases .
The first line of each test case has six integers , , , , , separated by spaces. is the number of rows of the board, is the number of columns, and is the number of faces of a die. , and are the scores described above.
The next lines give the rows of the board from the top row down. Each line is a string of length and gives the state of the dice in that row from the left. A fixed die is written as a single digit from 1 to , the number of the face it shows. A free die is written as a question mark ?.
Limits
- ,
- There is no limit on the number of free dice.
Output
For each test case print one line of the form Case #x: y, where is the test case number starting at 1 and is the expected score of that case. Round to seven digits after the decimal point and print exactly seven digits after the point.