📜  到达棋盘末端的预期移动次数|动态规划

📅  最后修改于: 2021-09-22 10:00:22             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程