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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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)
- Number of Unique BST with a given key | Dynamic Programming
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Total position where king can reach on a chessboard in exactly M moves
- Minimum moves to reach target on a infinite line | Set 2
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- 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
- Dynamic Programming on Trees | Set-1
- Dynamic Programming on Trees | Set 2
- Bitmasking and Dynamic Programming | Set-2 (TSP)
- Top 20 Dynamic Programming Interview Questions
- How to solve a Dynamic Programming Problem ?
- Dynamic Programming vs Divide-and-Conquer
- Double Knapsack | Dynamic Programming
- Convert N to M with given operations using dynamic programming
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.