给定N 个车站和三列火车A 、 B和C ,使得火车A在每个站停靠,火车B在每两个站停靠,火车C每三个站停靠,任务是找到到达该站的路数从第 1站可到达第 N站。
例子:
Input: N = 4
Output: 3
Explanation:
Below are the ways of reaching station 4 from station 1 as follows:
- Take train A at station 1 and continue to station 4 using only train A as (A → A → A → A).
- Take train B at station 1, stop at station 3 and take train A from station 3 to 4 as (B→ . → A).
- Take train C from station 1 and stop at station 4(C→ .)
Therefore, the total number of ways to reach station 4 from station 1 is 3.
Input: N = 15
Output: 338
方法:可以使用以下观察来解决给定的问题:
- 列车A可用于到达站X仅当列车A达到X – 1。
- 序列B可以用于达到站X仅当列车B到达X – 2。
- 列车C可用于到达站X仅当列车C到达X – 3。
因此,从上面的观察来看,思路是使用动态规划的概念。请按照以下步骤解决问题:
- 初始化一个二维数组DP使得:
- DP[i][1]存储使用列车 A到达车站 i 的方式数。
- DP[i][2]存储使用火车 B到达车站 i 的方式数量。
- DP[i][3]存储使用火车 C到达车站 i 的方式数量。
- DP[i][4]存储使用列车 A 、 B或C到达车站 i 的方式数。
- 到达站点 1 的方式只有一种。因此,将DP[1][1] , DP[1][2] , DP[1][3] , DP[1][4] 的值更新为1 。
- 使用上述观察,通过如下循环迭代更新每个状态的值:
- DP[i][1] = DP[i-1][4]
- DP[i][2] = DP[i-2][4]
- DP[i][3] = DP[i-3][4]
- DP[i][4] = DP[i][1] + DP[i][2] + DP[i][3]
- 完成以上步骤后,打印DP[N][4]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of ways
// to reach Nth station
int numberOfWays(int N)
{
// Declares the DP[] array
int DP[N + 1][5];
// Initialize dp[][] array
memset(DP, 0, sizeof(DP));
// Only 1 way to reach station 1
DP[1][1] = 1;
DP[1][2] = 1;
DP[1][3] = 1;
DP[1][4] = 1;
// Find the remaining states from
// the 2nd station
for (int i = 2; i <= N; i++) {
// If the train A is present
// at station i - 1
if (i - 1 > 0 && DP[i - 1][1] > 0)
DP[i][1] = DP[i - 1][4];
// If the train B is present
// at station i-2
if (i - 2 > 0 && DP[i - 2][2] > 0)
DP[i][2] = DP[i - 2][4];
// If train C is present at
// station i-3
if (i - 3 > 0 && DP[i - 3][3] > 0)
DP[i][3] = DP[i - 3][4];
// The total number of ways to
// reach station i
DP[i][4] = (DP[i][1] + DP[i][2]
+ DP[i][3]);
}
// Return the total count of ways
return DP[N][4];
}
// Driver Code
int main()
{
int N = 15;
cout << numberOfWays(N);
return 0;
}
Python3
# Python3 program for the above approach
# Function to find the number of ways
# to reach Nth station
def numberOfWays(N):
# Declares the DP[] array
DP = [[0 for i in range(5)]
for i in range(N + 1)]
# Initialize dp[][] array
# memset(DP, 0, sizeof(DP))
# Only 1 way to reach station 1
DP[1][1] = 1
DP[1][2] = 1
DP[1][3] = 1
DP[1][4] = 1
# Find the remaining states from
# the 2nd station
for i in range(2, N + 1):
# If the train A is present
# at station i - 1
if (i - 1 > 0 and DP[i - 1][1] > 0):
DP[i][1] = DP[i - 1][4]
# If the train B is present
# at station i-2
if (i - 2 > 0 and DP[i - 2][2] > 0):
DP[i][2] = DP[i - 2][4]
# If train C is present at
# station i-3
if (i - 3 > 0 and DP[i - 3][3] > 0):
DP[i][3] = DP[i - 3][4]
# The total number of ways to
# reach station i
DP[i][4] = (DP[i][1] + DP[i][2] + DP[i][3])
# Return the total count of ways
return DP[N][4]
# Driver Code
if __name__ == '__main__':
N = 15
print(numberOfWays(N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the number of ways
// to reach Nth station
static int numberOfWays(int N)
{
// Declares the DP[] array
int[,] DP = new int[N + 1, 5];
// Initialize dp[][] array
for(int i = 0; i < N + 1; i++)
{
for(int j = 0; j < 5; j++)
{
DP[i, j] = 0;
}
}
// Only 1 way to reach station 1
DP[1, 1] = 1;
DP[1, 2] = 1;
DP[1, 3] = 1;
DP[1, 4] = 1;
// Find the remaining states from
// the 2nd station
for(int i = 2; i <= N; i++)
{
// If the train A is present
// at station i - 1
if (i - 1 > 0 && DP[i - 1, 1] > 0)
DP[i, 1] = DP[i - 1, 4];
// If the train B is present
// at station i-2
if (i - 2 > 0 && DP[i - 2, 2] > 0)
DP[i, 2] = DP[i - 2, 4];
// If train C is present at
// station i-3
if (i - 3 > 0 && DP[i - 3, 3] > 0)
DP[i, 3] = DP[i - 3, 4];
// The total number of ways to
// reach station i
DP[i, 4] = (DP[i, 1] + DP[i, 2] + DP[i, 3]);
}
// Return the total count of ways
return DP[N, 4];
}
// Driver Code
static public void Main()
{
int N = 15;
Console.WriteLine(numberOfWays(N));
}
}
// This code is contributed by Dharanendra L V.
Javascript
// Javascript program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the number of ways
// to reach Nth station
static int numberOfWays(int N)
{
// Declares the DP[] array
int DP[][] = new int[N + 1][5];
// Initialize dp[][] array
for (int i = 0; i < N +1; i++) {
for (int j = 0; j < 5; j++) {
DP[i][j] = 0;
}
}
// Only 1 way to reach station 1
DP[1][1] = 1;
DP[1][2] = 1;
DP[1][3] = 1;
DP[1][4] = 1;
// Find the remaining states from
// the 2nd station
for (int i = 2; i <= N; i++) {
// If the train A is present
// at station i - 1
if (i - 1 > 0 && DP[i - 1][1] > 0)
DP[i][1] = DP[i - 1][4];
// If the train B is present
// at station i-2
if (i - 2 > 0 && DP[i - 2][2] > 0)
DP[i][2] = DP[i - 2][4];
// If train C is present at
// station i-3
if (i - 3 > 0 && DP[i - 3][3] > 0)
DP[i][3] = DP[i - 3][4];
// The total number of ways to
// reach station i
DP[i][4] = (DP[i][1] + DP[i][2]
+ DP[i][3]);
}
// Return the total count of ways
return DP[N][4];
}
// Driver Code
public static void main(String[] args)
{
int N = 15;
System.out.print(numberOfWays(N));
}
}
// This code is contributed by code_hunt.
输出:
338
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live