forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_paths.cpp
46 lines (41 loc) · 993 Bytes
/
unique_paths.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
/*
* Level : Easy
* Given a grid, calculate the maximum number of unique paths
* from source (0,0) to destination (m, n). You can only move
* either in down or in right direction.
*
* S . . . . . . . .
* . . . . . . . . .
* . . . . . . . . .
* . . . . . . . . .
* . . . . . . . . .
* . . . . . . . . D
*
* In this 6 x 9 grid,
* unique paths from source origin to destination are 1287.
*
*/
#include <iostream>
#include <vector>
int unique_paths(int m, int n)
{
std::vector<std::vector<int>> paths(m, std::vector<int>(n));
for (int i = 0; i < m; ++i) {
paths[i][0] = 1;
}
for (int j = 0; j < n; ++j) {
paths[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
paths[i][j] = paths[i-1][j] + paths[i][j-1];
}
}
return paths[m-1][n-1];
}
int main()
{
std::cout << "Number of unique paths for 6 x 9 grid: " <<
unique_paths(6, 9) << std::endl;
return 0;
}