Given a linear board of length N numbered from 1 to N, the task is to find the expected number of moves required to reach the Nth cell of the board, if we start at cell numbered 1 and at each step we roll a cubical dice to decide the next move. Also, we cannot go outside the bounds of the board. Note that the expected number of moves can be fractional.
Input: N = 8
p1 = (1 / 6) | 1-step -> 6 moves expected to reach the end
p2 = (1 / 6) | 2-steps -> 6 moves expected to reach the end
p3 = (1 / 6) | 3-steps -> 6 moves expected to reach the end
p4 = (1 / 6) | 4-steps -> 6 moves expected to reach the end
p5 = (1 / 6) | 5-steps -> 6 moves expected to reach the end
p6 = (1 / 6) | 6-steps -> 6 moves expected to reach the end
If we are 7 steps away, then we can end up at 1, 2, 3, 4, 5, 6 steps
away with equal probability i.e. (1 / 6).
Look at the above simulation to understand better.
dp[N – 1] = dp
= 1 + (dp + dp + dp + dp + dp + dp) / 6
= 1 + 6 = 7
Input: N = 10
Approach: This problem can be solved using dynamic programming. To solve the problem, decide the states of the DP first. One way will be to use the distance between the current cell and the Nth cell to define the states of DP. Let’s call this distance X. Thus dp[X] can be defined as the expected number of steps required to reach the end of the board of length X + 1 starting from the 1st cell.
Thus, the recurrence relation becomes:
dp[X] = 1 + (dp[X – 1] + dp[X – 2] + dp[X – 3] + dp[X – 4] + dp[X – 5] + dp[X – 6]) / 6
Now, for the base-cases:
dp = 0
Let’s try to calculate dp.
dp = 1 + 5 * (dp) / 6 + dp (Why? its because (5 / 6) is the probability it stays stuck at 1.)
dp / 6 = 1 (since dp = 0)
dp = 6
Similarly, dp = dp = dp = dp = dp = 6
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum number of moves to reach N starting from (1, 1)
- Count minimum number of moves to front or end to sort an array
- Expected Number of Trials until Success
- Expected number of coin flips to get two heads in a row?
- Expected Number of Trials to get N Consecutive Heads
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Minimum moves to reach target on a infinite line | Set 2
- Number of ways to place two queens on a N*N chess-board
- Count number of ways to jump to reach end
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Number of ways to reach the end of matrix with non-zero AND value
- Minimize the number of steps required to reach the end of the array
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum number of Fibonacci jumps to reach end
- Find minimum number of steps to reach the end of String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.