Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices.
Examples:
Input: N = 1
Output: 0
Explanation:
In 1 step, it is impossible to be again at position O.
Input: N = 2
Output: 3
Explanation:
The three ways to reach back to O in two steps are:
O->A->O
O->B->O
O->C->O
Input: N = 3
Output: 6
Explanation:
The 6 ways to reach back to O in three steps are:
O->A->B->O
O->A->C->O
O->B->A->O
O->B->C->O
O->C->A->O
O->C->B->O
Approach: The idea is to use the concept of Dynamic programming.
- A table T[][] is created where the row represents the number of ways and the column represents the position.
- In order to fill the table, one observation needs to be made. That is, we can go back to the position O if we are not at O in the previous step.
- Therefore, the number of ways to reach the origin O in the current step is equal to the sum of the number of ways the person is not at the origin O in the previous steps.
- Lets understand how the table is filled for 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
- The base case for this table is when N = 1. We can reach the origin in 1 step from all the positions except O.
Below is the implementation of the above approach:
Using Tabulation Approach
C++
#include <bits/stdc++.h>
using namespace std;
int fun( int n)
{
int sum=0;
for ( int i=1;i<n;i++)
{
sum=sum*3;
if (i%2)
{
sum+=3;
}
else
{
sum-=3;
}
}
return sum;
}
int main()
{
int n = 3;
cout << fun(n) << endl;
n = 4;
cout << fun(n) << endl;
return 0;
}
|
Java
class GFG{
static int count( int n)
{
if (n == 0 )
return 1 ;
if (n == 1 )
return 0 ;
int [][]dp = new int [ 4 ][n + 1 ];
dp[ 0 ][ 0 ] = 1 ;
dp[ 1 ][ 0 ] = 0 ;
dp[ 2 ][ 0 ] = 0 ;
dp[ 3 ][ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++)
{
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];
}
public static void main(String[] args)
{
int n = 3 ;
System.out.print(count(n) + "\n" );
n = 4 ;
System.out.print(count(n) + "\n" );
}
}
|
Python3
def count(n):
if (n = = 0 ):
return 1
if (n = = 1 ):
return 0
dp = [[ 0 for i in range (n + 1 )]
for j in range ( 4 )]
dp[ 0 ][ 0 ] = 1
dp[ 1 ][ 0 ] = 0
dp[ 2 ][ 0 ] = 0
dp[ 3 ][ 0 ] = 0
for i in range ( 1 , n + 1 ):
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]
if __name__ = = "__main__" :
n = 3
print (count(n))
n = 4
print (count(n))
|
C#
using System;
class GFG{
static int count( int n)
{
if (n == 0)
return 1;
if (n == 1)
return 0;
int [,]dp = new int [4, n + 1];
dp[0, 0] = 1;
dp[1, 0] = 0;
dp[2, 0] = 0;
dp[3, 0] = 0;
for ( int i = 1; i <= n; i++)
{
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];
}
public static void Main(String[] args)
{
int n = 3;
Console.Write(count(n) + "\n" );
n = 4;
Console.Write(count(n) + "\n" );
}
}
|
Javascript
<script>
function count(n)
{
if (n == 0)
return 1;
if (n == 1)
return 0;
let dp = new Array(4);
for (let i = 0; i < 4; i++)
{
dp[i] = new Array(n + 1);
for (let j = 0; j < (n + 1); j++)
{
dp[i][j] = 0;
}
}
dp[0][0] = 1;
dp[1][0] = 0;
dp[2][0] = 0;
dp[3][0] = 0;
for (let i = 1; i <= n; i++)
{
let countPositionO = dp[1][i - 1] +
dp[2][i - 1] +
dp[3][i - 1];
let countPositionA = dp[0][i - 1] +
dp[2][i - 1] +
dp[3][i - 1];
let countPositionB = dp[0][i - 1] +
dp[1][i - 1] +
dp[3][i - 1];
let 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];
}
let n = 3;
document.write(count(n) + "<br>" );
n = 4;
document.write(count(n) + "<br>" );
</script>
|
Time Complexity: O(N).
Auxiliary Space Complexity: O(1)
Note:
- This program works more efficiently to find the number of ways in constant time after preprocessing for multiple queries if we fill the table for the largest number among the set of queries..
Last Updated :
26 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...