forked from jugshaurya/recursion-CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratInAMaze.cpp
51 lines (44 loc) · 1.25 KB
/
ratInAMaze.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
#include <iostream>
using namespace std;
void ratInAMaze(int maze[][20], int n, int output[][20], int row, int col){
if(row==n-1 && col == n-1){
//print output
output[row][col] = 1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<output[i][j] <<" ";
}
cout<<endl;
}
cout<<endl;
output[row][col]=0;
return;
}
output[row][col] = 1;
//UP -Direction
if(row-1 >=0 && output[row-1][col]!=1 && maze[row-1][col]!=0){
ratInAMaze(maze,n,output,row-1,col);
}
//down -Direction
if(row+1<n && output[row+1][col]!=1 && maze[row+1][col]!=0){
ratInAMaze(maze,n,output,row+1,col);
}
//right -Direction
if(col+1 <n && output[row][col+1]!=1 && maze[row][col+1]!=0){
ratInAMaze(maze,n,output,row,col+1);
}
//left -Direction
if(col-1>=0 && output[row][col-1]!=1 && maze[row][col-1]!=0){
ratInAMaze(maze,n,output,row,col-1);
}
output[row][col] = 0;
}
void ratInAMaze(int maze[][20], int n){
int output[20][20] = {};
output[0][0] = 1;
ratInAMaze(maze, n, output,0,0);
}
int main() {
int maze[20][20]= {{1,0,1},{1,1,1},{1,1,1}};
ratInAMaze(maze,3);
}