📜  预计将达到董事会结束的举动数量|动态编程

📅  最后修改于: 2021-06-25 18:12:49             🧑  作者: Mango

给定长度为N的1N的线性板,任务是找到到达板的N单元所需的预期移动数,如果我们从编号为1的单元开始,并且每一步都滚动一个立方骰子决定下一步行动。而且,我们不能走出董事会的界限。请注意,预期的移动次数可以是分数。
例子:

方法:可以使用动态编程解决此问题。要解决该问题,请先确定DP的状态。一种方法是使用当前单元格与第N单元格之间的距离来定义DP的状态。将此距离称为X。因此DP [X]作为步骤的预期数量需要达到长度X + 1第一单元开始的基板的端部可以限定。
因此,递归关系变为:

现在,对于基本情况:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#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;
}


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


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


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


Javascript


输出:
7.36111