forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRat_in_a_maize.cpp
58 lines (53 loc) · 1.24 KB
/
Rat_in_a_maize.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
#include<bits/stdc++.h>
int maze[100][100];
int ar[100][100];
void explore(int n,int row,int col)
{
if(row==n-1 and col==n-1)
{
//base case
maze[row][col]=1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<maze[i][j]<<" ";
cout<<endl;
maze[row][col]=0;
return;
}
if(col+1!=n and ar[row][col+1]!=0 and maze[row][col+1]!=1)
{
maze[row][col+1]=1;
explore(n,row,col+1);
maze[row][col+1]=0;
}
if(col-1!=-1 and ar[row][col-1]!=0 and maze[row][col-1]!=1)
{
maze[row][col-1]=1;
explore(n,row,col-1);
maze[row][col-1]=0;
}
if(row+1!=n and ar[row+1][col]!=0 and maze[row+1][col]!=1)
{
maze[row+1][col]=1;
explore(n,row+1,col);
maze[row+1][col]=0;
}
if(row-1!=-1 and ar[row-1][col]!=0 and maze[row-1][col]!=1)
{
maze[row-1][col]=1;
explore(n,row-1,col);
maze[row-1][col]=0;
}
}
void findpath(int n)
{
memset(maze,0,sizeof(maze));
maze[0][0]=1;
explore(n,0,0);
}
void ratInAMaze(int arr[][20], int n){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ar[i][j]=arr[i][j];
findpath(n);
}