-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRatNMaze.cpp
More file actions
66 lines (59 loc) · 1.82 KB
/
RatNMaze.cpp
File metadata and controls
66 lines (59 loc) · 1.82 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
64
65
66
//author @Nishant
#include<bits/stdc++.h>
using namespace std;
void showPath(int **sol, int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << " " << sol[i][j] << " ";
cout << endl;
}
}
bool isValidPlace(int **mat, int N, int x, int y) { //function to check place is inside the maze and have value 1
if(x >= 0 && x < N && y >= 0 && y < N && mat[x][y] == 1)
return true;
return false;
}
bool solveRatMaze(int **sol, int **mat, int N, int x, int y) {
if(x == N-1 && y == N-1) { //when (x,y) is the bottom right room
sol[x][y] = 1;
return true;
}
if(isValidPlace(mat, N, x, y) == true) { //check whether (x,y) is valid or not
sol[x][y] = 1; //set 1, when it is valid place
if (solveRatMaze(sol, mat, N, x+1, y) == true) //find path by moving right direction
return true;
if (solveRatMaze(sol, mat, N, x, y+1) == true) //when x direction is blocked, go for bottom direction
return true;
sol[x][y] = 0; //if both are closed, there is no path
return false;
}
return false;
}
bool findSolution(int **sol, int **mat, int N) {
if(solveRatMaze(sol, mat, N, 0, 0) == false) {
cout << "There is no path";
return false;
}
cout << "\nPath to get out of maze" << endl;
showPath(sol, N);
return true;
}
int main() {
int N;
cout << "Enter n value:";
cin >> N;
int **mat = (int **)malloc(N * sizeof(int *));
for(int i=0; i<N; i++){
mat[i] = (int *)malloc(N * sizeof(int));
}
int **sol = (int **)malloc(N * sizeof(int *));
for(int i=0; i<N; i++){
sol[i] = (int *)malloc(N * sizeof(int));
}
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
cin >> mat[i][j];
}
}
findSolution(sol, mat, N);
}