📜  四面体中长度为N的不同循环路径的数量

📅  最后修改于: 2021-04-24 20:44:15             🧑  作者: Mango

给定四面体(顶点为A,B,C,D),任务是从顶点查找长度为n的不同循环路径的数量。

注意:仅考虑单个顶点B,即找到从B到自身长度为N的不同循环路径的数量。

例子:

Input: 2
Output: 3
The paths of length 2 which starts and ends at D are:
B-A-B
B-D-B
B-C-B

Input: 3
Output: 6

方法:动态编程可用于跟踪N的先前值的路径数。检查在路径中移动时剩下的移动数以及我们在哪里。那就是4n个州,每个州有3个选择。观察到所有顶点A,B,C都是相等的。设zB最初为1,以0步为步长,我们只能到达B本身。令zACD为1作为到达其他顶点A,C和D的路径为0。因此,形成的递归关系将是:

在每个步骤中, zADC都乘以2(2个状态),并与zB相加,因为zB是步骤n-1中的路径数,其中包括剩余的2个状态。

下面是上述方法的实现:

C++
// C++ program count total number of
// paths to reach B from B
#include 
#include 
using namespace std;
  
// Function to count the number of
// steps in a tetrahedron
int countPaths(int n)
{
    // initially coming to B is B->B
    int zB = 1;
  
    // cannot reach A, D or C
    int zADC = 0;
  
    // iterate for all steps
    for (int i = 1; i <= n; i++) {
  
        // recurrence relation
        int nzB = zADC * 3;
  
        int nzADC = (zADC * 2 + zB);
  
        // memoize previous values
        zB = nzB;
        zADC = nzADC;
    }
  
    // returns steps
    return zB;
}
  
// Driver Code
int main()
{
    int n = 3;
    cout << countPaths(n);
  
    return 0;
}


Java
// Java program count total 
// number of paths to reach
// B from B
import java.io.*;
  
class GFG 
{
      
// Function to count the
// number of steps in a
// tetrahedron
static int countPaths(int n)
{
    // initially coming 
    // to B is B->B
    int zB = 1;
  
    // cannot reach A, D or C
    int zADC = 0;
  
    // iterate for all steps
    for (int i = 1; i <= n; i++) 
    {
  
        // recurrence relation
        int nzB = zADC * 3;
  
        int nzADC = (zADC * 2 + zB);
  
        // memoize previous values
        zB = nzB;
        zADC = nzADC;
    }
  
    // returns steps
    return zB;
}
  
// Driver Code
public static void main (String[] args) 
{
    int n = 3;
    System.out.println(countPaths(n));
}
}
  
// This code is contributed by ajit


Python3
# Python3 program count total number of
# paths to reach B from B
  
# Function to count the number of
# steps in a tetrahedron
def countPaths(n):
      
    # initially coming to B is B->B
    zB = 1
  
    # cannot reach A, D or C
    zADC = 0
  
    # iterate for all steps
    for i in range(1, n + 1): 
  
        # recurrence relation
        nzB = zADC * 3
  
        nzADC = (zADC * 2 + zB)
  
        # memoize previous values
        zB = nzB
        zADC = nzADC
      
    # returns steps
    return zB
  
# Driver code
n = 3
print(countPaths(n))
  
# This code is contributed by ashutosh450


C#
// C# program count total 
// number of paths to reach 
// B from B 
using System;
  
class GFG{
          
// Function to count the 
// number of steps in a 
// tetrahedron 
static int countPaths(int n) 
{ 
      
    // initially coming 
    // to B is B->B 
    int zB = 1; 
  
    // cannot reach A, D or C 
    int zADC = 0; 
  
    // iterate for all steps 
    for (int i = 1; i <= n; i++) 
    { 
  
        // recurrence relation 
        int nzB = zADC * 3; 
  
        int nzADC = (zADC * 2 + zB); 
  
        // memoize previous values 
        zB = nzB; 
        zADC = nzADC; 
    } 
  
    // returns steps 
    return zB; 
} 
  
    // Driver Code 
    static public void Main ()
    {
        int n = 3; 
        Console.WriteLine(countPaths(n)); 
    } 
} 
  
// This code is contributed by Sach


PHP
B 
    $zB = 1; 
  
    // cannot reach A, D or C 
    $zADC = 0; 
  
    // iterate for all steps 
    for ($i = 1; $i <= $n; $i++)
    { 
  
        // recurrence relation 
        $nzB = $zADC * 3; 
  
        $nzADC = ($zADC * 2 + $zB); 
  
        // memoize previous values 
        $zB = $nzB; 
        $zADC = $nzADC; 
    } 
  
    // returns steps 
    return $zB; 
} 
  
// Driver Code 
$n = 3; 
echo countPaths($n); 
  
  
// This code is contributed 
// by Sachin
?>


输出:
6