📜  计算从起始石到达终点的方式,每一步最多跳 K

📅  最后修改于: 2021-09-06 06:10:57             🧑  作者: Mango

从左到右连续给定N块石头。从每块石头,你最多可以跳到K块石头。任务是找到办法从某物石到第N石达到总数。
例子:

方法:

  1. 假设dp[i]是到达第 i 个石头的方法数。
  2. 因为最多有K 次跳跃,所以第i 个石头可以被它之前的所有K石头到达。
  3. 迭代所有可能的K 次跳跃,并继续将此可能的组合添加到数组dp[]
  4. 那么从第s块石头到达第 N 个节点的可能方法总数将是dp[N-1]
  5. 例如:

下面是上述方法的实现:

C++
// C++ program to find total no.of ways
// to reach nth step
#include "bits/stdc++.h"
using namespace std;
 
// Function which returns total no.of ways
// to reach nth step from sth steps
int TotalWays(int n, int s, int k)
{
    // Initialize dp array
    int dp[n];
 
    // filling all the elements with 0
    memset(dp, 0, sizeof(dp));
 
    // Initialize (s-1)th index to 1
    dp[s - 1] = 1;
 
    // Iterate a loop from s to n
    for (int i = s; i < n; i++) {
 
        // starting range for counting ranges
        int idx = max(s - 1, i - k);
 
        // Calculate Maximum moves to
        // Reach ith step
        for (int j = idx; j < i; j++) {
            dp[i] += dp[j];
        }
    }
 
    // For nth step return dp[n-1]
    return dp[n - 1];
}
 
// Driver Code
int main()
{
    // no of steps
    int n = 5;
 
    // Atmost steps allowed
    int k = 2;
 
    // starting range
    int s = 2;
    cout << "Total Ways = "
         << TotalWays(n, s, k);
}


Java
// Java program to find total no.of ways
// to reach nth step
class GFG{
  
// Function which returns total no.of ways
// to reach nth step from sth steps
static int TotalWays(int n, int s, int k)
{
    // Initialize dp array
    int []dp = new int[n];
  
    // Initialize (s-1)th index to 1
    dp[s - 1] = 1;
  
    // Iterate a loop from s to n
    for (int i = s; i < n; i++) {
  
        // starting range for counting ranges
        int idx = Math.max(s - 1, i - k);
  
        // Calculate Maximum moves to
        // Reach ith step
        for (int j = idx; j < i; j++) {
            dp[i] += dp[j];
        }
    }
  
    // For nth step return dp[n-1]
    return dp[n - 1];
}
  
// Driver Code
public static void main(String[] args)
{
    // no of steps
    int n = 5;
  
    // Atmost steps allowed
    int k = 2;
  
    // starting range
    int s = 2;
    System.out.print("Total Ways = "
         + TotalWays(n, s, k));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python 3 program to find total no.of ways
# to reach nth step
 
# Function which returns total no.of ways
# to reach nth step from sth steps
def TotalWays(n, s, k):
 
    # Initialize dp array
    dp = [0]*n
 
    # Initialize (s-1)th index to 1
    dp[s - 1] = 1
 
    # Iterate a loop from s to n
    for i in range(s, n):
 
        # starting range for counting ranges
        idx = max(s - 1, i - k)
 
        # Calculate Maximum moves to
        # Reach ith step
        for j in range( idx, i) :
            dp[i] += dp[j]
 
    # For nth step return dp[n-1]
    return dp[n - 1]
 
# Driver Code
if __name__ == "__main__":
    # no of steps
    n = 5
 
    # Atmost steps allowed
    k = 2
 
    # starting range
    s = 2
    print("Total Ways = ", TotalWays(n, s, k))
     
# This code is contributed by chitranayal


C#
// C# program to find total no.of ways
// to reach nth step
using System;
 
class GFG{
      
    // Function which returns total no.of ways
    // to reach nth step from sth steps
    static int TotalWays(int n, int s, int k)
    {
        // Initialize dp array
        int []dp = new int[n];
      
        // Initialize (s-1)th index to 1
        dp[s - 1] = 1;
      
        // Iterate a loop from s to n
        for (int i = s; i < n; i++) {
      
            // starting range for counting ranges
            int idx = Math.Max(s - 1, i - k);
      
            // Calculate Maximum moves to
            // Reach ith step
            for (int j = idx; j < i; j++) {
                dp[i] += dp[j];
            }
        }
      
        // For nth step return dp[n-1]
        return dp[n - 1];
    }
      
    // Driver Code
    public static void Main(string[] args)
    {
        // no of steps
        int n = 5;
      
        // Atmost steps allowed
        int k = 2;
      
        // starting range
        int s = 2;
        Console.Write("Total Ways = "+ TotalWays(n, s, k));
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
Total Ways = 3

时间复杂度: O(N 2 ),其中 N 是石头的数量。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live