给定一个整数N ,表示位于源和目的地之间的站点数。每个车站有3列火车可供选择,它们的停靠模式如下:
- 火车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
方法:解决问题的主要思路是使用Recursion with Memoization来解决这个问题。递推关系如下:
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)