Number of paths
PROBLEM :
The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move only to right or down.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is M and N, M is number of rows and N is number of columns.
Output:
Print the number of paths.
Constraints:
1 = T = 30
1 = M,N = 10
Example:
Input
2
3 3
2 8
Output
6
8
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Number_of_paths(int ,int ) ;
int main()
{
int t,m,n ;
cin>>t ;
while(t--)
{
cin>>m>>n ;
cout<<Number_of_paths(m,n)<<endl ;
}
return 0;
}
int Number_of_paths(int m,int n)
{
int mat[m][n] ;
int i,j ;
for(i=0;i<m;i++)
mat[i][0]=1 ;
for(i=0;i<n;i++)
mat[0][i]=1 ;
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
mat[i][j]=mat[i-1][j]+mat[i][j-1] ;
}
return mat[m-1][n-1] ;
}
---------------------------------------------------------------------------------
The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move only to right or down.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is M and N, M is number of rows and N is number of columns.
Output:
Print the number of paths.
Constraints:
1 = T = 30
1 = M,N = 10
Example:
Input
2
3 3
2 8
Output
6
8
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Number_of_paths(int ,int ) ;
int main()
{
int t,m,n ;
cin>>t ;
while(t--)
{
cin>>m>>n ;
cout<<Number_of_paths(m,n)<<endl ;
}
return 0;
}
int Number_of_paths(int m,int n)
{
int mat[m][n] ;
int i,j ;
for(i=0;i<m;i++)
mat[i][0]=1 ;
for(i=0;i<n;i++)
mat[0][i]=1 ;
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
mat[i][j]=mat[i-1][j]+mat[i][j-1] ;
}
return mat[m-1][n-1] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment