Better Call Paul II
On his trip to Japan, Milan wanted to try his chances at pachinko — a Japanese game that can be likened to a vertical game of pinball. By turning a knob, you can control the strength at which you shoot small metal balls into the machine. These metal balls fall down columns and bounce off obstacles until they fall into a gate, with the gate into which your ball falls determining your winnings.
Previously, Milan was struggling to win until his friend, Paul, told him the expected winnings of each column. With this in mind, Milan was able to strategically turn the knob just right so that he could get the metal balls into his desired column. He was slowly winning some, but still making a net loss.
Just as he has thoughts of giving up, Paul comes back around.
"Uh, so I forgot to account for the obstacles earlier", he says, as he pats Milan on the shoulder.
"Oh what? Come on man!" sighs Milan as he stares as his last yen.
Paul explains that the pachinko machine can be modeled as follows: you can drop the ball in any column and it falls down until it either reaches the bottom of the machine (which results in no wins), a
gate (indicated by a number to
, which results in that win), or an obstacle (indicated by
an asterisk). If a ball hits an obstacle, it proceeds falling in the column to the left or to the right of the obstacle, with
probability. No two obstacles or gates are adjacent, not even diagonally, and none are located in the leftmost or rightmost column.
To maximise his expected winnings, which column should Milan aim to get his balls into?
Input
The first line contains two integers and
, the width and height of the pachinko machine.
The next lines contain
characters describing the pachinko machine, with
.
denoting an empty space, *
denoting an obstacle, and through to
denoting the winning gates.
Output
On the first line, output the maximal expected winnings with either an absolute or a relative error of at most .
On the second line, output a single integer, the column with the maximum expected winnings. The column is -indexed.
Clarifications
- You are guaranteed to get a non-zero maximal expected value.
Example
Input 1
7 5
.....
.1...
...2.
.*...
.....
.....
..5..
Output 1
5.0000000
3
In this case, the optimal column that Milan should aim for is column (
-indexed). Since there are no obstacles in the way, he can go straight for this column, which has a maximal expected value of
.
Input 2
8 8
...1....
........
..*...*.
....*...
.1......
...*.*..
........
..9.7.7.
Output 2
7.5000000
5
If we choose column , we encounter a first obstacle which has a
probability of going left or right. Then, we encounter an obstacle on either side, which splits the probabilities once again. Then, we reach either the gates that have
,
or
.
The probability of getting these gates (given the previously encountered obstacles) are ,
, and
respectively. Using this, we can calculate the maximal expected winnings as follows:
Hence is our maximal expected winnings and column
is what Milan should choose.
Input 3
10 10
.*.*.*.*..
..........
..*.*.*.*.
..........
.*.*.*.*..
..........
..*.*.*.*.
..........
.*.*.*.*..
....9.....
Output 3
3.3750000
5
Comments