-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossible_paths.cpp
More file actions
37 lines (35 loc) · 828 Bytes
/
Possible_paths.cpp
File metadata and controls
37 lines (35 loc) · 828 Bytes
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
#include <iostream>
using namespace std;
// Program for counting possible number of paths
int countPaths(int s, int e)
{
// Time Complexity : O(2^n)
// Space Complexity : O(2^n)
if (s == e)
return 1;
if (s > e)
return 0;
int count = 0;
for (int i = 1; i <= 6; i++)
{
count += countPaths(s + i, e);
}
return count;
}
// Program for counting possible numbebr of paths in a 2D board
int countPathsMaze(int n, int i, int j)
{
// Time Complexity : O(2^n)
// Space Complexity : O(2^n)
if (i == n - 1 && j == n - 1)
return 1;
if (i >= n || j >= n)
return 0;
return countPathsMaze(n, i + 1, j) + countPathsMaze(n, i, j + 1);
}
int main()
{
cout << countPaths(0, 3) << endl;
cout << countPathsMaze(3, 0, 0) << endl;
return 0;
}