forked from k-samarth/Data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze_Backtracking
More file actions
41 lines (38 loc) · 921 Bytes
/
Copy pathMaze_Backtracking
File metadata and controls
41 lines (38 loc) · 921 Bytes
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
#include <iostream>
using namespace std;
bool pathfinder(int maze[4][4],int path[4][4],int i,int j)
{
if(i==3 && j==3)
{
path[i][j]=1;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
cout<<path[i][j]<<" ";}
cout<<endl;}
cout<<endl<<endl;
return false;
}
if(i>=4 || j>=4 ||i<0 ||j<0|| path[i][j]==1 || maze[i][j]==0)
return false;
path[i][j]=1;
if(pathfinder(maze,path,i,j+1)){
return true;}
if(pathfinder(maze,path,i+1,j)){
return true;}
if(pathfinder(maze,path,i-1,j)){
return true;}
if(pathfinder(maze,path,i,j-1)){
return true;}
path[i][j]=0;
return false;
}
int main()
{
int maze[4][4]={{1,1,1,0},
{1,1,0,0},
{0,1,1,0},
{0,1,1,1}};
int path[4][4]={0};
cout<<pathfinder(maze,path,0,0)<<endl;
return 0;
}