C++ Program for Maneuvering a cave (TCS Codevita) | PrepInsta

Cpp Program for Maneuvering a cave

Maneuvering a Cave Problem

Maneuvering a Cave Problem is a 2-D array manipulation problem, which was asked in this year TCS CodeVita 2020 Season 9 coding competition, as a sample question. It is a pretty tough problem, but one can solve it if he has good command on data structures and dynamic programming. Here we have provided a Python Solution for this problem.

 

Problem Description

The task is to count all the possible paths from top left to bottom right of a m x n matrix with the constraints that from each cell you can either move only to right or down.

Input: 

  • First line consists of T test cases. First line of every test case consists of N and M, denoting the number of rows and number of columns respectively.

Output: 

  • Single line output i.e count of all the possible paths from top left to bottom right of a m x n matrix..

Constraints:

  • 1<=T<=100
  • 1<=N<=100
  • 1<=M<=100

C++ Code

#include  <iostream>
using namespace std;
int calc(int x, int y) ;
 
int main()
{
	int a,b,n;
	cout<<("Enter the number of test cases");
	cin>>n;
	while(n!=0)
	{
	cout<<("Enter the number of rows of the  matrix : ");
	cin>>a;
	cout<<("Enter the number of columns of the matrix : ");
	cin>>b;
	cout << "Number of ways - "<< calc(a,b)<<"\n";
	n--;
	}
		return  0;

}
int calc(int x, int y)
{
	if (x == 1 || y == 1)// If there is a singular matrix
    	{
        	return  1;
    	}
    	else
    	{
        	return calc(x - 1, y) + calc(x, y - 1);
    	}
    
}
Output
Enter the number of test cases 
2
Enter the number of rows of the matrix : 2
Enter the number of columns of the matrix : 2
Number of ways - 2
Enter the number of rowa of the matrix : 3
Enter the number of columns of the matrix : 3
Number of ways - 6

Maneuvering a cave Problem in Other Coding Languages

Python

To find the solution of Maneuvering a Cave Problem in Python Programming language click on the button below

 Python

C

To find the solution of Maneuvering a Cave problem in C Programming language click on the button  below:

C

Java

To find the solution of Maneuvering a cave problem in Java Programming language click on the button below:

Java