-
Notifications
You must be signed in to change notification settings - Fork 2
/
maze_problem_counting_paths.cpp
134 lines (104 loc) · 3.06 KB
/
maze_problem_counting_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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************************************************
File name: maze_problem_counting_paths.cpp
Author: babajr
*****************************************************************************/
/*
A robot is located at the top-left corner of a m x n grid.
The robot can only move either down or right at any point in time.
The robot is trying to reach the bottom-right corner of the grid.
How many possible unique paths are there?
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down
- -
| | |
- -
| | |
- -
| | |
- -
*/
#include <bits/stdc++.h>
using namespace std;
#define ROW_SIZE 3
#define COL_SIZE 3
/*
API to count the number of paths from grid point (0,0) to (ROW, COL).
Allowed Directions: Down (Vertical), Right (Horizontal)
*/
int count_paths(int r_size, int c_size)
{
if(r_size == 1 || c_size == 1)
return 1;
return (count_paths(r_size - 1, c_size) + count_paths(r_size, c_size - 1));
}
/*
API to print the paths from the start to end of the matrix/maze.
Allowed Directions: Down (Vertical), Right (Horizontal)
*/
void print_paths(string path, int r_size, int c_size)
{
if(r_size == 1 && c_size == 1)
{
cout << path << endl;
return;
}
if(r_size > 1)
print_paths(path + "D", r_size - 1, c_size);
if(c_size > 1)
print_paths(path + "R", r_size, c_size - 1);
}
/*
API to print the paths from start to end of maze.
Allowed Directions: Down (Vertical), Right (Horizontal) & Diagonal
*/
void print_paths_diagonal(string path, int r_size, int c_size)
{
if(r_size == 1 && c_size == 1)
{
cout << path << endl;
return;
}
if(r_size > 1 && c_size > 1) // diagonal
print_paths(path + "D", r_size - 1, c_size - 1);
if(r_size > 1) // down
print_paths(path + "V", r_size - 1, c_size);
if(c_size > 1) // right
print_paths(path + "H", r_size, c_size - 1);
}
/*
API to print the paths with some restrictions.
*/
void print_paths_with_restrictions(bool maze[][COL_SIZE], string path, int r, int c)
{
if(r == ROW_SIZE - 1 && c == COL_SIZE - 1)
{
cout << path << endl;
return;
}
if(maze[r][c] == false)
return;
if(r < ROW_SIZE - 1)
print_paths_with_restrictions(maze, path + "D", r + 1, c);
if(c < COL_SIZE - 1)
print_paths_with_restrictions(maze, path + "R", r, c + 1);
}
int main(void)
{
// printf("Number of paths: %d\n", count_paths(ROW_SIZE, COL_SIZE));
// print_paths("", ROW_SIZE, COL_SIZE);
// print_paths_diagonal("", ROW_SIZE, COL_SIZE);
// count paths with some restrictions.
// false ==> you can not go to that cell
bool maze[ROW_SIZE][COL_SIZE] = {
{true, true, true},
{true, false, true},
{true, true, true}
};
print_paths_with_restrictions(maze, "", 0, 0);
return 0;
}