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] ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )