Number of paths in a matrix with k coins
PROBLEM :
Given a matrix where every cell has some number of coins. Count number of ways to reach bottom right from top left with exactly k coins. We can move to (i+1, j) and (i, j+1) from a cell (i, j).
Input:
First line contains number of test cases T. For each test case, first line contains the integer value 'k' denoting coins, second line contains an integer 'N' denoting the order of square matrix.Last line contains NXN elements in a single line in row-major order.
Output:
Number of paths are displayed as output to the user. '0' is displayed when no path is found.
Constraints:
1 <=T<= 100
1 <=k<= 20
1 <=N<= 10
1 <=arr[i]<= 100
Example:
Input:
2
16
3
1 2 3 4 6 5 9 8 7
12
3
1 2 3 4 6 5 3 2 1
Output:
0
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 10
int Number_paths_matrix(int [MAX][MAX],int ,int ,int ) ;
int main()
{
int t,k,no,i,j ;
int mat[MAX][MAX] ;
cin>>t ;
while(t--)
{
cin>>k ;
cin>>no ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cin>>mat[i][j] ;
no=Number_paths_matrix(mat,no-1,no-1,k) ;
cout<<no<<endl ;
}
return 0;
}
int Number_paths_matrix(int mat[MAX][MAX],int row,int col,int k)
{
if((row==0&&col==0)&&(k==mat[row][col]))
return 1 ;
else if(row<0||col<0)
return 0 ;
return Number_paths_matrix(mat,row-1,col,k-mat[row][col])+Number_paths_matrix(mat,row,col-1,k-mat[row][col]) ;
}
---------------------------------------------------------------------------------
Given a matrix where every cell has some number of coins. Count number of ways to reach bottom right from top left with exactly k coins. We can move to (i+1, j) and (i, j+1) from a cell (i, j).
Input:
First line contains number of test cases T. For each test case, first line contains the integer value 'k' denoting coins, second line contains an integer 'N' denoting the order of square matrix.Last line contains NXN elements in a single line in row-major order.
Output:
Number of paths are displayed as output to the user. '0' is displayed when no path is found.
Constraints:
1 <=T<= 100
1 <=k<= 20
1 <=N<= 10
1 <=arr[i]<= 100
Example:
Input:
2
16
3
1 2 3 4 6 5 9 8 7
12
3
1 2 3 4 6 5 3 2 1
Output:
0
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 10
int Number_paths_matrix(int [MAX][MAX],int ,int ,int ) ;
int main()
{
int t,k,no,i,j ;
int mat[MAX][MAX] ;
cin>>t ;
while(t--)
{
cin>>k ;
cin>>no ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cin>>mat[i][j] ;
no=Number_paths_matrix(mat,no-1,no-1,k) ;
cout<<no<<endl ;
}
return 0;
}
int Number_paths_matrix(int mat[MAX][MAX],int row,int col,int k)
{
if((row==0&&col==0)&&(k==mat[row][col]))
return 1 ;
else if(row<0||col<0)
return 0 ;
return Number_paths_matrix(mat,row-1,col,k-mat[row][col])+Number_paths_matrix(mat,row,col-1,k-mat[row][col]) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment