1456. Expected Board Finish
A travelling game board has cells numbered 1 to n. A single token starts on cell 1, and each turn a fair six-sided die is rolled and the token moves forward by that many cells. If the roll would carry the token past cell n, the token stays where it is for that turn. Each entry [s, e] of jumps is a shortcut or a trap: when the token lands on cell s it is immediately moved to cell e, which may be ahead or behind. The token finishes on reaching cell n. Return the expected number of rolls needed to finish. An absolute or relative error of 1e-6 is accepted. Setting up a linear system and solving it by elimination takes O(n^3) time and O(n^2) space.
Example 1
- Input:
- n = 7, jumps = []
- Output:
- 5.999999999999995
- Explanation:
With no jumps, the token needs to reach cell 7 from cell 1 using a die that can overshoot, giving the plain waiting-time expectation.
Example 2
- Input:
- n = 10, jumps = [[3,8]]
- Output:
- 7.166666666666661
- Explanation:
Landing on cell 3 teleports the token to cell 8, which shortens the expected wait compared with an empty board.
Example 3
- Input:
- n = 10, jumps = [[9,2]]
- Output:
- 8.599999999999996
- Explanation:
Landing on cell 9 sends the token back to cell 2, which lengthens the expected wait.
Constraints
7 ≤ n ≤ 100
0 ≤ jumps.length ≤ 20; every s is distinct and lies in [2, n - 1]; no two jump starts are adjacent cells; no jump end equals any jump start; 1 ≤ e ≤ n and e ≠ s
Absolute or relative error 1e-6.
How this problem is judged
- Answers
- Numbers are accepted within a tolerance of 1.0E-6: |answer - expected| <= 1.0E-6 x max(1, |expected|).
- Tolerance
0.000001
Expected complexity
- Time
- O(n^3)
- Space
- O(n^2)