Skip to content

Commit e06dd61

Browse files
committed
Robot matrix navigation using backtracking
1 parent cebe8e3 commit e06dd61

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

backtracking/02-robot-matrix.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
//----------------------------------------
3+
// - Question: Given a matrix and start and ending positions, help a robot navigate through the matrix and print all possible paths.
4+
// The matrix might have obstacles in which case the current path must be abandoned and a new path (if available) must be searched.
5+
// At every cell of the matrix, the robot can move down one step or move right one step or move diagonally down right one step
6+
7+
#include <iostream>
8+
#include <vector>
9+
#include <iterator> // ostream_iterator
10+
using namespace std;
11+
12+
// macro to define limits for the input matrix
13+
#define MAX_X 4
14+
#define MAX_Y 9
15+
16+
// coordinates of the robot destination cell
17+
#define END_X 3
18+
#define END_Y 8
19+
20+
struct Point
21+
{
22+
int x;
23+
int y;
24+
};
25+
26+
bool isValid(int current_x, int current_y)
27+
{
28+
return current_x >= 0 && current_x < MAX_X && current_y >= 0 && current_y < MAX_Y;
29+
}
30+
31+
ostream& operator<<(ostream& output, const Point& p)
32+
{
33+
output << "(" << p.x << "," << p.y << ")";
34+
return output;
35+
}
36+
37+
void printCurrentPath(const vector<Point>& currentPath)
38+
{
39+
copy(currentPath.begin(), currentPath.end(), ostream_iterator<Point>(cout, " "));
40+
cout << endl;
41+
}
42+
43+
Point neighbors[]{{1, 1}, {1, 0}, {0, 1}};
44+
45+
// recursive helper function that implements backtracking
46+
void printPathsHelper(
47+
int M[MAX_X][MAX_Y],
48+
int current_x,
49+
int current_y,
50+
vector<Point> & currentPath)
51+
{
52+
if (!(isValid(current_x, current_y) && M[current_x][current_y] == 0))
53+
{
54+
return;
55+
}
56+
57+
currentPath.push_back(Point{current_x, current_y});
58+
59+
if (current_x == END_X && current_y == END_Y)
60+
{
61+
printCurrentPath(currentPath);
62+
}
63+
else
64+
{
65+
M[current_x][current_y] = 1;
66+
for (auto neighbor : neighbors)
67+
{
68+
int next_x = current_x + neighbor.x;
69+
int next_y = current_y + neighbor.y;
70+
printPathsHelper(M, next_x, next_y, currentPath);
71+
}
72+
M[current_x][current_y] = 0;
73+
}
74+
75+
currentPath.pop_back();
76+
}
77+
78+
// driver function for the solution
79+
void printPaths(
80+
int M[MAX_X][MAX_Y],
81+
int start_x,
82+
int start_y)
83+
{
84+
vector<Point> currentPath;
85+
printPathsHelper(M, start_x, start_y, currentPath);
86+
}
87+
88+
int main(int argc, char *argv[])
89+
{
90+
int start_x = 0, start_y = 0;
91+
int M[MAX_X][MAX_Y]=
92+
{
93+
// 0 means the position/ path is available for exploring.
94+
// 1 means it is a wall/ obstacle and we cannot proceed further.
95+
{0,0,0,1,1,1,0,0,0},
96+
{1,1,0,0,0,0,0,0,0},
97+
{1,0,1,0,0,1,0,1,0},
98+
{0,0,1,1,0,1,1,1,0}
99+
};
100+
101+
printPaths(M, start_x, start_y);
102+
103+
return 0;
104+
}
105+
106+
/*
107+
Output:
108+
(0,0) (0,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (2,8) (3,8)
109+
(0,0) (0,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (2,8) (3,8)
110+
(0,0) (0,1) (0,2) (1,3) (1,4) (1,5) (1,6) (1,7) (2,8) (3,8)
111+
(0,0) (0,1) (0,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (2,8) (3,8)
112+
(0,0) (0,1) (0,2) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (2,8) (3,8)
113+
(0,0) (0,1) (0,2) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (2,8) (3,8)
114+
115+
*/
116+

0 commit comments

Comments
 (0)