-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathratInmaze.cpp
More file actions
47 lines (45 loc) · 1.08 KB
/
ratInmaze.cpp
File metadata and controls
47 lines (45 loc) · 1.08 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
#include<iostream>
using namespace std;
void helper(int maze[][20], int **sol, int n, int r, int c)
{
if(r == n-1 && c == n-1){
sol[r][c] =1;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cout << sol[i][j] << " ";
}
}
cout << endl;
return;
}
// if(r>=n && c>=n && sol[r+1][c]==1 && sol[r-1][c]==1 && sol[r][c+1]==1 && sol[r][c-1]==1){
// return;
// }
if(r>=n || c>=n || r<0 || c<0 || maze[r][c]==0 || sol[r][c]==1){
return;
}
sol[r][c] = 1;
helper(maze, sol, n, r-1, c); //4 possible directions
helper(maze, sol, n, r+1, c);
helper(maze, sol, n, r, c-1);
helper(maze, sol, n, r, c+1);
sol[r][c] = 0;
}
void ratInAMaze(int maze[][20], int n){
int **sol = new int*[n]{0};
for(int i=0; i<n; i++){
sol[i] = new int[n]{0};
}
helper(maze, sol, n, 0, 0);
}
int main(){
int n;
cin >> n ;
int maze[20][20];
for(int i = 0; i < n ;i++){
for(int j = 0; j < n; j++){
cin >> maze[i][j];
}
}
ratInAMaze(maze, n);
}