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 **N ^{th}** 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.

**Examples:**

Input:N = 8

Output:7

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[7]

= 1 + (dp[1] + dp[2] + dp[3] + dp[4] + dp[5] + dp[6]) / 6

= 1 + 6 = 7

Input:N = 10

Output:7.36111

**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 **N ^{th}** 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

**1**cell.

^{st}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] = 0

Let’s try to calculate dp[1].

dp[1] = 1 + 5 * (dp[1]) / 6 + dp[0] (Why? its because (5 / 6) is the probability it stays stuck at 1.)

dp[1] / 6 = 1 (since dp[0] = 0)

dp[1] = 6

Similarly, dp[1] = dp[2] = dp[3] = dp[4] = dp[5] = 6

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define maxSize 50 ` `using` `namespace` `std; ` ` ` `// To store the states of dp ` `double` `dp[maxSize]; ` ` ` `// To determine whether a state ` `// has been solved before ` `int` `v[maxSize]; ` ` ` `// Function to return the count ` `double` `expectedSteps(` `int` `x) ` `{ ` ` ` ` ` `// Base cases ` ` ` `if` `(x == 0) ` ` ` `return` `0; ` ` ` `if` `(x <= 5) ` ` ` `return` `6; ` ` ` ` ` `// If a state has been solved before ` ` ` `// it won't be evaluated again ` ` ` `if` `(v[x]) ` ` ` `return` `dp[x]; ` ` ` ` ` `v[x] = 1; ` ` ` ` ` `// Recurrence relation ` ` ` `dp[x] = 1 + (expectedSteps(x - 1) + ` ` ` `expectedSteps(x - 2) + ` ` ` `expectedSteps(x - 3) + ` ` ` `expectedSteps(x - 4) + ` ` ` `expectedSteps(x - 5) + ` ` ` `expectedSteps(x - 6)) / 6; ` ` ` `return` `dp[x]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `cout << expectedSteps(n - 1); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `static` `int` `maxSize = ` `50` `; ` ` ` ` ` `// To store the states of dp ` ` ` `static` `double` `dp[] = ` `new` `double` `[maxSize]; ` ` ` ` ` `// To determine whether a state ` ` ` `// has been solved before ` ` ` `static` `int` `v[] = ` `new` `int` `[maxSize]; ` ` ` ` ` `// Function to return the count ` ` ` `static` `double` `expectedSteps(` `int` `x) ` ` ` `{ ` ` ` ` ` `// Base cases ` ` ` `if` `(x == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `if` `(x <= ` `5` `) ` ` ` `return` `6` `; ` ` ` ` ` `// If a state has been solved before ` ` ` `// it won't be evaluated again ` ` ` `if` `(v[x] == ` `1` `) ` ` ` `return` `dp[x]; ` ` ` ` ` `v[x] = ` `1` `; ` ` ` ` ` `// Recurrence relation ` ` ` `dp[x] = ` `1` `+ (expectedSteps(x - ` `1` `) + ` ` ` `expectedSteps(x - ` `2` `) + ` ` ` `expectedSteps(x - ` `3` `) + ` ` ` `expectedSteps(x - ` `4` `) + ` ` ` `expectedSteps(x - ` `5` `) + ` ` ` `expectedSteps(x - ` `6` `)) / ` `6` `; ` ` ` ` ` `return` `dp[x]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `System.out.println(expectedSteps(n - ` `1` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `maxSize ` `=` `50` ` ` `# To store the states of dp ` `dp ` `=` `[` `0` `] ` `*` `maxSize ` ` ` `# To determine whether a state ` `# has been solved before ` `v ` `=` `[` `0` `] ` `*` `maxSize ` ` ` `# Function to return the count ` `def` `expectedSteps(x): ` ` ` ` ` `# Base cases ` ` ` `if` `(x ` `=` `=` `0` `): ` ` ` `return` `0` ` ` `if` `(x <` `=` `5` `): ` ` ` `return` `6` ` ` ` ` `# If a state has been solved before ` ` ` `# it won't be evaluated again ` ` ` `if` `(v[x]): ` ` ` `return` `dp[x] ` ` ` ` ` `v[x] ` `=` `1` ` ` ` ` `# Recurrence relation ` ` ` `dp[x] ` `=` `1` `+` `(expectedSteps(x ` `-` `1` `) ` `+` ` ` `expectedSteps(x ` `-` `2` `) ` `+` ` ` `expectedSteps(x ` `-` `3` `) ` `+` ` ` `expectedSteps(x ` `-` `4` `) ` `+` ` ` `expectedSteps(x ` `-` `5` `) ` `+` ` ` `expectedSteps(x ` `-` `6` `)) ` `/` `6` ` ` `return` `dp[x] ` ` ` `# Driver code ` `n ` `=` `10` ` ` `print` `(` `round` `(expectedSteps(n ` `-` `1` `), ` `5` `)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `maxSize = 50; ` ` ` ` ` `// To store the states of dp ` ` ` `static` `double` `[]dp = ` `new` `double` `[maxSize]; ` ` ` ` ` `// To determine whether a state ` ` ` `// has been solved before ` ` ` `static` `int` `[]v = ` `new` `int` `[maxSize]; ` ` ` ` ` `// Function to return the count ` ` ` `static` `double` `expectedSteps(` `int` `x) ` ` ` `{ ` ` ` ` ` `// Base cases ` ` ` `if` `(x == 0) ` ` ` `return` `0; ` ` ` ` ` `if` `(x <= 5) ` ` ` `return` `6; ` ` ` ` ` `// If a state has been solved before ` ` ` `// it won't be evaluated again ` ` ` `if` `(v[x] == 1) ` ` ` `return` `dp[x]; ` ` ` ` ` `v[x] = 1; ` ` ` ` ` `// Recurrence relation ` ` ` `dp[x] = 1 + (expectedSteps(x - 1) + ` ` ` `expectedSteps(x - 2) + ` ` ` `expectedSteps(x - 3) + ` ` ` `expectedSteps(x - 4) + ` ` ` `expectedSteps(x - 5) + ` ` ` `expectedSteps(x - 6)) / 6; ` ` ` ` ` `return` `dp[x]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `Console.WriteLine(expectedSteps(n - 1)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

7.36111

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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.org. 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.