给定整数N ,它表示源和目的地之间的站点数。每个站都有三列火车,其停车方式如下:
- 火车1:在每个车站都停靠
- 火车2:在每个备用车站都停靠
- 火车3:每三站一站
任务是找到使用任何可能的火车组合从源头到达目的地的方法。
例子:
Input: N = 2
Output: 4
Explanation:
Four possible ways exists to travel from source to destination with 2 stations in between:
Train 1 (from source) -> Train 1 (from station 1) -> Train 1(from station 2) -> Destination
Train 2 (from source) -> Train 1 (from station 2) -> Destination
Train 1 (from source) -> Train 2 (from station 1) -> Destination
Train 3 (from source) -> Destination
Input: N = 0
Output: 1
Explanation: No station is present in between the source and destination. Therefore, there is only one way to travel, i.e.
Train 1(from source) -> Destination
方法:解决该问题的主要思想是使用“带记忆的递归”来解决此问题。递归关系如下:
F(N) = F(N – 1) + F(N – 2) + F(N – 3),
where,
F(N – 1) counts ways to travel upto (N – 1)th station.
F(N – 2) counts ways to travel upto (N – 2)th station.
F(N – 3) counts ways to travel upto (N – 3)th station.
请按照以下步骤解决问题:
- 初始化数组dp []进行存储。最初将所有索引设置为-1 。
- 定义一个递归函数findWays()来计算到达第N个站点的方式数量。
- 需要考虑以下基本情况:
- 如果x <0,则返回0。
- 对于x = 0 ,返回1。
- 对于x = 1 ,返回2。
- 对于x = 2,返回4。
- 如果当前状态(例如x )已被评估,即dp [x]不等于-1 ,则只需返回评估值。
- 否则,递归计算findWays(x – 1) , findWays(x – 2)和findWays(x – 3)并将它们的和存储在dp [x]中。
- 返回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)