-
Notifications
You must be signed in to change notification settings - Fork 0
/
rat_in_a_maze.cpp
63 lines (54 loc) · 1.13 KB
/
rat_in_a_maze.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
#include<iostream>
using namespace std;
int isavail(int i, int j, int arr[4][4], int n) {
if (arr[i][j] == 1 && i<n && j<n && i >= 0 && j >= 0)
return 1;
else
return 0;
}
int path(int sol[4][4], int i, int j, int arr[4][4], int n) {
if (i == n - 1 && j == n - 1) {
sol[i][j] = 1;
return 1;
}
if (isavail(i, j, arr, n)==1) {
sol[i][j] = 1;
if (path(sol, i + 1, j, arr, n)==1)
return 1;
else if (path(sol, i, j + 1, arr, n)==1)
return 1;
else
sol[i][j] = 0;
// return false;
}
return 0;
}
void findpath(int arr[4][4], int n) {
int sol[4][4]={0};
if (path(sol, 0, 0, arr, n) == 0) {
cout << "\nno path possible.";
return;
}
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++)
cout << sol[i][j] << "\t";
cout << "\n";
}
return;
}
int main() {
int n;
cout << "\nenter the size of your matrix:";
cin >> n;
int arr[4][4];
for (int k = 0; k < n; k++)
for (int l = 0; l < n; l++)
cin >> arr[k][l];
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++)
cout << arr[i][j] << "\t";
cout << "\n";
}
findpath(arr, n);
return 0;
}