-
Notifications
You must be signed in to change notification settings - Fork 0
/
acg_backtracking_rat_in_maze.cpp
85 lines (62 loc) · 1.57 KB
/
acg_backtracking_rat_in_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
using namespace std;
bool isSafe(int arr[][5],int x ,int y,int n){
// cout<<"in safe"<<endl;
if(x<n && y<n && arr[x][y] == 1 ){
// cout<<"element "<<x<<","<<y<<" is safe"<<endl;
return true;
}
return false;
}
bool ratinmaze(int arr[][5],int x, int y,int n, int **res){
if(x == n-1 && y == n-1){
res[x][y] =1;
return true;
}
if(isSafe(arr,x,y,n)){
res[x][y] =1;
if(ratinmaze(arr,x+1,y,n,res)){
return true;
}
if(ratinmaze(arr,x,y+1,n,res)){
return true;
}
res[x][y] =0; //backtracking
return false;
}
return false;
}
int main()
{
int n=5;
// cin>>n;
// maze array (0 means the block is full and 1 means it is empty ans so is traversable)
// int**arr = new int*[n]; // dynamicalyy alocating 2d array;
// // notice 2 astresik;
// for (int i = 0; i < n; i++)
// {
// arr[i] = new int[n](); // allocating each row of abovve array;
// }
int arr[5][5]= {{1,0,1,0,1},{1,1,1,1,1},{0,1,0,1,0},{1,0,0,1,1},{1,1,1,0,1}};
// int ** ptr = arr;
// path followed array
int**res = new int*[n];
for (int i = 0; i < n; i++)
{
res[i] = new int[n]();
}
int t =ratinmaze((arr),0,0,n,res);
cout<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout<<res[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<endl;
return 0;
}