-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_NumOfPaths.cpp
81 lines (67 loc) · 1.41 KB
/
GFG_NumOfPaths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
https://practice.geeksforgeeks.org/problems/number-of-paths0926/1#
Number of paths
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
int rows, cols;
long long pathsCnt=0;
void countPaths(int r, int c)
{
if(r==rows-1 && c==cols-1)
pathsCnt++;
else if(r==rows-1) // downmost
countPaths(r, c+1);
else if(c==cols-1) //rightmost
countPaths(r+1, c);
else {
countPaths(r, c+1); //right
countPaths(r+1, c); //down
}
}
long long numberOfPaths(int m, int n)
{
//2-D DP
// long long dp[m][n];
// for(int c=0; c<n; c++)
// dp[0][c]=1;
// for(int r=0; r<m; r++)
// dp[r][0]=1;
// for(int i=1; i<m; i++)
// {
// for(int j=1; j<n; j++)
// dp[i][j] = dp[i][j-1] + dp[i-1][j];
// }
// return dp[m-1][n-1];
//1-D DP
// long long dp[n];
// for(int r=0; r<n; r++)
// dp[r]=1;
// for(int r=1; r<m; r++)
// {
// for(int c=1; c<n; c++)
// dp[c] = dp[c] + dp[c-1];
// }
// return dp[n-1];
//Backtracking
rows = m;
cols = n;
pathsCnt=0;
countPaths(0,0);
return pathsCnt;
}
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>m>>n;
cout << numberOfPaths(m, n)<<endl;
}
return 0;
} // } Driver Code Ends