📜  火车站| TCS CodeVita 2020

📅  最后修改于: 2021-04-17 16:02:28             🧑  作者: Mango

给定整数N ,它表示源和目的地之间的站点数。每个站都有三列火车,其停车方式如下:

  • 火车1:在每个车站都停靠
  • 火车2:在每个备用车站都停靠
  • 火车3:每三站一站

任务是找到使用任何可能的火车组合从源头到达目的地的方法。

例子:

方法:解决该问题的主要思想是使用“带记忆的递归”来解决此问题。递归关系如下:

请按照以下步骤解决问题:

  1. 初始化数组dp []进行存储。最初将所有索引设置为-1
  2. 定义一个递归函数findWays()来计算到达第N站点的方式数量。
  3. 需要考虑以下基本情况:
    • 如果x <0,则返回0。
    • 对于x = 0 ,返回1。
    • 对于x = 1 ,返回2。
    • 对于x = 2,返回4。
  4. 如果当前状态(例如x )已被评估,即dp [x]不等于-1 ,则只需返回评估值。
  5. 否则,递归计算findWays(x – 1)findWays(x – 2)findWays(x – 3)并将它们的和存储在dp [x]中
  6. 返回dp [x]

下面是上述方法的实现:

C++14
// C++ program of the above approach
#include 
using namespace std;
 
// Dp table for memoization
int dp[100000];
 
// Function to count the number
// of ways to N-th station
int findWays(int x)
{
    // Base Cases
    if (x < 0)
        return 0;
 
    if (x == 0)
        return 1;
 
    if (x == 1)
        return 2;
 
    if (x == 2)
        return 4;
 
    // If current state is
    // already evaluated
    if (dp[x] != -1)
        return dp[x];
 
    // Recursive calls
 
    // Count ways in which
    // train 1 can be chosen
    int count = findWays(x - 1);
 
    // Count ways in which
    // train 2 can be chosen
    count += findWays(x - 2);
 
    // Count ways in which
    // train 3 can be chosen
    count += findWays(x - 3);
 
    // Store the current state
    dp[x] = count;
 
    // Return the number of ways
    return dp[x];
}
 
// Driver Code
int main()
{
 
    // Given Input
    int n = 4;
 
    // Initialize DP table with -1
    memset(dp, -1, sizeof(dp));
 
    // Function call to count
    // the number of ways to
    // reach the n-th station
    cout << findWays(n) << "\n";
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Dp table for memoization
  static int dp[] = new int[100000];
 
  // Function to count the number
  // of ways to N-th station
  static int findWays(int x)
  {
 
    // Base Cases
    if (x < 0)
      return 0;
 
    if (x == 0)
      return 1;
 
    if (x == 1)
      return 2;
 
    if (x == 2)
      return 4;
 
    // If current state is
    // already evaluated
    if (dp[x] != -1)
      return dp[x];
 
    // Recursive calls
 
    // Count ways in which
    // train 1 can be chosen
    int count = findWays(x - 1);
 
    // Count ways in which
    // train 2 can be chosen
    count += findWays(x - 2);
 
    // Count ways in which
    // train 3 can be chosen
    count += findWays(x - 3);
 
    // Store the current state
    dp[x] = count;
 
    // Return the number of ways
    return dp[x];
  }
 
  // Driven Code
  public static void main(String[] args)
  {
 
    // Given Input
    int n = 4;
 
    // Initialize DP table with -1
    Arrays.fill(dp, -1);
 
    // Function call to count
    // the number of ways to
    // reach the n-th station
    System.out.print(findWays(n));
  }
}
 
// This code is contributed by splevel62.


Python3
# Python3 program of the above approach
 
# Dp table for memoization
dp = [-1 for i in range(100000)]
 
# Function to count the number
# of ways to N-th station
def findWays(x):
     
    # Base Cases
    if (x < 0):
        return 0
 
    if (x == 0):
        return 1
 
    if (x == 1):
        return 2
 
    if (x == 2):
        return 4
 
    # If current state is
    # already evaluated
    if (dp[x] != -1):
        return dp[x]
 
    # Recursive calls
 
    # Count ways in which
    # train 1 can be chosen
    count = findWays(x - 1)
 
    # Count ways in which
    # train 2 can be chosen
    count += findWays(x - 2)
 
    # Count ways in which
    # train 3 can be chosen
    count += findWays(x - 3)
 
    # Store the current state
    dp[x] = count
 
    # Return the number of ways
    return dp[x]
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    n = 4
     
    # Function call to count
    # the number of ways to
    # reach the n-th station
    print(findWays(n))
 
# This code is contributed by SURENDRA_GANGWAR


输出:
13

时间复杂度: O(N)
辅助空间: O(N)