Count all possible paths from top left to bottom right of a mXn matrix

PROBLEM :

Given a M X N matrix with initial position at top-left corner, find the number of possible unique paths to reach the bottom right corner of the grid from the initial position.


NOTE: Possible moves can be either down or right at any point in time, i.e., we can move to matrix[i+1][j] or matrix[i][j+1] from matrix[i][j].


Input:

The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an two integers A and B depicting the size of the grid.

Output:

Print the number of unique paths to reach bottom-right corner from the top-left corner.


Constraints:

1 = T = 30
1 = M = 15
1 = N = 15

Example:

Input
2
2 2
3 4
Output
2
10

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Number_of_Unique_Paths(int ,int ) ;
int main()
 {
int t,m,n,no ;
cin>>t ;
while(t--)
{
   cin>>m>>n ;
   no=Number_of_Unique_Paths(m,n) ;
   cout<<no<<endl ;
}
return 0;
}

int Number_of_Unique_Paths(int m,int n)
{
    int mat[m][n] ;
    int i,j ;
   
    for(i=0;i<n;i++)
        mat[0][i]=1 ;
   
    for(j=0;j<m;j++)
        mat[j][0]=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

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 )