forked from AggHim/SRIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatInAMaze.cpp
67 lines (58 loc) · 1.23 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Input:
3
1 0 1
1 1 1
1 1 1
Output:
1 0 0 1 1 1 1 1 1
1 0 0 1 0 0 1 1 1
1 0 0 1 1 0 0 1 1
1 0 0 1 1 1 0 0 1
*/
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
bool check(int row, int col, int n){
if(row>=0 && row<n && col>=0 && col<n){
return true;
}
return false;
}
void print(int path[][20], int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << path[i][j] << " ";
}
}
cout << endl;
}
void ratInAMazeHelper(int maze[][20], int path[][20], int n, int r, int c){
if(r==n-1 && c==n-1){
path[r][c]=1;
print(path,n);
path[r][c]=0;
return;
}
for(int i=0;i<4;i++){
int r1 = r + dx[i];
int c1 = c + dy[i];
if(check(r1,c1,n) && path[r1][c1]==0 && maze[r1][c1]==1){
path[r][c]=1;
ratInAMazeHelper(maze,path,n,r1,c1);
path[r][c]=0;
}
}
}
void ratInAMaze(int maze[][20], int n){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Print output as specified in the question
*/
int path[n][20];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
path[i][j]=0;
}
}
ratInAMazeHelper(maze,path,n,0,0);
}