📜  在三角金字塔中以 N 步循环路径的方式计数

📅  最后修改于: 2021-09-07 02:05:21             🧑  作者: Mango

给定一个三角形金字塔,其顶点标记为O、A、B 和 C ,数字为N ,任务是找到使从原点O开始的人最初以 N 步返回原点的方式数。在一个步骤中,一个人可以去到任何相邻的顶点。

三角金字塔

例子:

方法:这个想法是使用动态规划的概念。

  1. 创建一个表T[][] ,其中行代表路数,列代表位置。
  2. 为了填满表格,需要进行一项观察。也就是说,如果我们在上一步中不在 O 处,我们可以回到位置 O。
  3. 因此,当前步骤到达原点O的路数等于前面步骤中人不在原点O的路数之和。
  4. 让我们了解 N = 3 的表格是如何填充的:
0   1    2   3
O   1   0    3   6
A   0   1    2   7
B   0   1    2   7
C   0   1    2   7
  1. 该表的基本情况是当 N = 1 时。 我们可以从除 O 之外的所有位置在 1 步内到达原点。

下面是上述方法的实现:
使用制表方法

C++
// C++ program for Dynamic
// Programming implementation of
// Number of Path in a Triangular
// pyramid
 
#include 
using namespace std;
 
// Function to return the number of
// ways we can reach back to the
// initial position O
int count(int n)
{
    // If n is 0 then there is
    // 1 solution
    if (n == 0)
        return 1;
 
    // If n is equal to 1
    // then we can't reach at position O
    if (n == 1)
        return 0;
 
    int dp[4][n + 1];
 
    // Initial Conditions
 
    // Represents position O
    dp[0][0] = 1;
 
    // Represents position A
    dp[1][0] = 0;
 
    // Represents position B
    dp[2][0] = 0;
 
    // Represents position C
    dp[3][0] = 0;
 
    // Filling the table
    for (int i = 1; i <= n; i++) {
 
        // The number of ways to reach
        // a particular position (say X)
        // at the i'th step is equivalent
        // to the sum of the number
        // of ways the person is not at
        // position X in the last step.
 
        int countPositionO
            = dp[1][i - 1] + dp[2][i - 1]
              + dp[3][i - 1];
 
        int countPositionA
            = dp[0][i - 1] + dp[2][i - 1]
              + dp[3][i - 1];
 
        int countPositionB
            = dp[0][i - 1] + dp[1][i - 1]
              + dp[3][i - 1];
 
        int countPositionC
            = dp[0][i - 1] + dp[1][i - 1]
              + dp[2][i - 1];
 
        dp[0][i] = countPositionO;
        dp[1][i] = countPositionA;
        dp[2][i] = countPositionB;
        dp[3][i] = countPositionC;
    }
 
    return dp[0][n];
}
 
// Driver code
int main()
{
 
    int n = 3;
    cout << count(n) << endl;
 
    n = 4;
    cout << count(n) << endl;
 
    return 0;
}


Java
// Java program for dynamic programming
// implementation of number of path in
// a triangular pyramid
class GFG{
 
// Function to return the number of
// ways we can reach back to the
// initial position O
static int count(int n)
{
     
    // If n is 0 then there is
    // 1 solution
    if (n == 0)
        return 1;
 
    // If n is equal to 1 then we
    // can't reach at position O
    if (n == 1)
        return 0;
 
    int [][]dp = new int[4][n + 1];
 
    // Initial Conditions
 
    // Represents position O
    dp[0][0] = 1;
 
    // Represents position A
    dp[1][0] = 0;
 
    // Represents position B
    dp[2][0] = 0;
 
    // Represents position C
    dp[3][0] = 0;
 
    // Filling the table
    for(int i = 1; i <= n; i++)
    {
        
       // The number of ways to reach
       // a particular position (say X)
       // at the i'th step is equivalent
       // to the sum of the number
       // of ways the person is not at
       // position X in the last step.
       int countPositionO = dp[1][i - 1] +
                            dp[2][i - 1] +
                            dp[3][i - 1];
       int countPositionA = dp[0][i - 1] +
                            dp[2][i - 1] +
                            dp[3][i - 1];
       int countPositionB = dp[0][i - 1] +
                            dp[1][i - 1] +
                            dp[3][i - 1];
       int countPositionC = dp[0][i - 1] +
                            dp[1][i - 1] +
                            dp[2][i - 1];
        
       dp[0][i] = countPositionO;
       dp[1][i] = countPositionA;
       dp[2][i] = countPositionB;
       dp[3][i] = countPositionC;
    }
    return dp[0][n];
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.print(count(n) + "\n");
 
    n = 4;
    System.out.print(count(n) + "\n");
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program for Dynamic
# Programming implementation of
# Number of Path in a Triangular
# pyramid
 
# Function to return the number of
# ways we can reach back to the
# initial position O
def count(n):
 
    # If n is 0 then there is
    # 1 solution
    if (n == 0):
        return 1
 
    # If n is equal to 1
    # then we can't reach at position O
    if (n == 1):
        return 0
 
    dp = [[0 for i in range(n + 1)]
             for j in range(4)]
 
    # Initial Conditions
 
    # Represents position O
    dp[0][0] = 1
 
    # Represents position A
    dp[1][0] = 0
 
    # Represents position B
    dp[2][0] = 0
 
    # Represents position C
    dp[3][0] = 0
 
    # Filling the table
    for i in range(1, n + 1):
 
        # The number of ways to reach
        # a particular position (say X)
        # at the i'th step is equivalent
        # to the sum of the number
        # of ways the person is not at
        # position X in the last step.
 
        countPositionO = (dp[1][i - 1] +
                          dp[2][i - 1] +
                          dp[3][i - 1])
 
        countPositionA = (dp[0][i - 1] +
                          dp[2][i - 1] +
                          dp[3][i - 1])
 
        countPositionB = (dp[0][i - 1] +
                          dp[1][i - 1] +
                          dp[3][i - 1])
 
        countPositionC = (dp[0][i - 1] +
                          dp[1][i - 1] +
                          dp[2][i - 1])
 
        dp[0][i] = countPositionO
        dp[1][i] = countPositionA
        dp[2][i] = countPositionB
        dp[3][i] = countPositionC
     
    return dp[0][n]
 
# Driver code
if __name__ == "__main__":
 
    n = 3
    print(count(n))
 
    n = 4
    print(count(n))
 
# This code is contributed by ChitraNayal


C#
// C# program for dynamic programming
// implementation of number of path in
// a triangular pyramid
using System;
 
class GFG{
 
// Function to return the number
// of ways we can reach back to
// the initial position O
static int count(int n)
{
     
    // If n is 0 then there is
    // 1 solution
    if (n == 0)
        return 1;
 
    // If n is equal to 1 then we
    // can't reach at position O
    if (n == 1)
        return 0;
 
    int [,]dp = new int[4, n + 1];
 
    // Initial Conditions
 
    // Represents position O
    dp[0, 0] = 1;
 
    // Represents position A
    dp[1, 0] = 0;
 
    // Represents position B
    dp[2, 0] = 0;
 
    // Represents position C
    dp[3, 0] = 0;
 
    // Filling the table
    for(int i = 1; i <= n; i++)
    {
         
       // The number of ways to reach
       // a particular position (say X)
       // at the i'th step is equivalent
       // to the sum of the number
       // of ways the person is not at
       // position X in the last step.
       int countPositionO = dp[1, i - 1] +
                            dp[2, i - 1] +
                            dp[3, i - 1];
       int countPositionA = dp[0, i - 1] +
                            dp[2, i - 1] +
                            dp[3, i - 1];
       int countPositionB = dp[0, i - 1] +
                            dp[1, i - 1] +
                            dp[3, i - 1];
       int countPositionC = dp[0, i - 1] +
                            dp[1, i - 1] +
                            dp[2, i - 1];
        
       dp[0, i] = countPositionO;
       dp[1, i] = countPositionA;
       dp[2, i] = countPositionB;
       dp[3, i] = countPositionC;
    }
    return dp[0, n];
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    Console.Write(count(n) + "\n");
 
    n = 4;
    Console.Write(count(n) + "\n");
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
6
21

时间复杂度: O(N)。
辅助空间复杂度: O(4*N)
笔记:

  • 如果我们为查询集中的最大数量填充表,则该程序可以更有效地在对多个查询进行预处理后在恒定时间内找到方法的数量。
  • 对于单个查询,该程序还可以更节省空间。这个想法是,由于我们只需要前一步值来计算当前步长值,因此只需取 4 个变量来存储前一步值,我们就可以在恒定空间中解决给定的问题。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live