forked from jugshaurya/recursion-CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratInAMaze_2.cpp
51 lines (40 loc) · 867 Bytes
/
ratInAMaze_2.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<bits/stdc++.h>
using namespace std;
void printoutput(int** output,int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << output[i][j] << " ";
}
}
cout<<endl;
}
void mazeHelp(int maze[][20],int n,int** output,int x,int y){
if(x == n-1 && y == n-1){
output[x][y] =1;
printoutput(output,n);
output[x][y] =0;
return;
}
if(x>=n || x<0 || y>=n || y<0 || maze[x][y] ==0 || output[x][y] ==1){
return;
}
output[x][y] = 1;
mazeHelp(maze,n,output,x-1,y);
mazeHelp(maze,n,output,x+1,y);
mazeHelp(maze,n,output,x,y-1);
mazeHelp(maze,n,output,x,y+1);
output[x][y] = 0;
}
void ratInAMaze(int maze[][20], int n){
int** output = new int*[n];
for(int i=0;i<n;i++){
output[i] = new int[n];
}
mazeHelp(maze,n,output,0,0);
for(int i=0;i<n;i++){
delete [] output[i] ;
}
delete [] output;
}
int main(){
}