forked from MainakRepositor/500-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
119.cpp
39 lines (33 loc) · 1.04 KB
/
119.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
// A C++ program to count all possible paths
// from top left to bottom right
#include <iostream>
using namespace std;
// Returns count of possible paths to reach cell at
// row number m and column number n from the topmost
// leftmost cell (cell at 1, 1)
int numberOfPaths(int m, int n)
{
// Create a 2D table to store results of subproblems
int count[m][n];
// Count of paths to reach any cell in first column is 1
for (int i = 0; i < m; i++)
count[i][0] = 1;
// Count of paths to reach any cell in first row is 1
for (int j = 0; j < n; j++)
count[0][j] = 1;
// Calculate count of paths for other cells in
// bottom-up manner using the recursive solution
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++)
// By uncommenting the last part the code calculates the total
// possible paths if the diagonal Movements are allowed
count[i][j] = count[i - 1][j] + count[i][j - 1]; //+ count[i-1][j-1];
}
return count[m - 1][n - 1];
}
// Driver program to test above functions
int main()
{
cout << numberOfPaths(3, 3);
return 0;
}