-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratInMaze-I.cc
68 lines (55 loc) · 1.51 KB
/
ratInMaze-I.cc
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
/**
* Consider a rat placed at (0, 0) in a square matrix of order n and with a
* destination to reach at (n-1, n-1). Find one possible path to do so.
* Store the path in a binary output matrix. The directions in which the
* rat can move are right and down only.
*/
#include <bits/stdc++.h>
using namespace std;
bool isValid(int n, int x, int y, vector<vector<int>>& in) {
if (x >= 0 && x < n && y >=0 && y < n && in[x][y] == 1)
return true;
return false;
}
bool findPathHelper(int n, int x, int y, auto& out, auto& in) {
if (x == n - 1 && y == n -1) {
out[x][y] = 1;
return true;
} else {
if (isValid(n, x, y, in)) {
out[x][y] = 1;
// Explore righward path
if (findPathHelper(n, x+1, y, out, in))
return true;
// Explore downward path
if (findPathHelper(n, x, y+1, out, in))
return true;
// If path not found yet then backtrack i.e. unmark x, y
out[x][y] = 0;
return false;
}
return false;
}
}
void findPath(int n, vector<vector<int>>& in) {
// Output matrix with all cells initialised to zero
vector<vector<int>> outMaze(n, vector<int>(n, 0));
findPathHelper(n, 0, 0, outMaze, in);
// Print output matrix with solution path
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << outMaze[i][j] << ' ';
cout << endl;
}
}
int main() {
int n;
cin >> n;
vector<vector<int>> inMaze(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> inMaze[i][j];
findPath(n, inMaze);
return 0;
}
// #TODO: Analyse time complexity!