📜  火车站 | TCS CodeVita 2020

📅  最后修改于: 2021-09-17 07:45:59             🧑  作者: Mango

给定一个整数N ,表示位于源和目的地之间的站点数。每个车站有3列火车可供选择,它们的停靠模式如下:

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

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

例子:

方法:解决问题的主要思路是使用Recursion with Memoization来解决这个问题。递推关系如下:

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

  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)