-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmazerat.cpp
More file actions
63 lines (50 loc) · 1.19 KB
/
mazerat.cpp
File metadata and controls
63 lines (50 loc) · 1.19 KB
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
#include<iostream>
using namespace std;
void printPath(char path[][10], int m, int n) {
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
cout << path[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void f(char maze[][10], char path[][10], int m, int n, int i, int j) {
// boundary + block + visited check
if(i < 0 || j < 0 || i >= m || j >= n || maze[i][j] == 'X' || path[i][j] == '1') {
return;
}
// destination reached
if(i == m - 1 && j == n - 1) {
path[i][j] = '1';
printPath(path, m, n);
path[i][j] = '0';
return;
}
// mark current cell
path[i][j] = '1';
// move in all 4 directions
f(maze, path, m, n, i, j + 1); // right
f(maze, path, m, n, i + 1, j); // down
f(maze, path, m, n, i, j - 1); // left
f(maze, path, m, n, i - 1, j); // up
// backtrack
path[i][j] = '0';
}
int main() {
char maze[][10] = {
"0000",
"00X0",
"000X",
"0X00"
};
char path[][10] = {
"0000",
"0000",
"0000",
"0000"
};
int m = 4, n = 4;
f(maze, path, m, n, 0, 0);
return 0;
}