forked from sethupathib/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findPath.cpp
72 lines (54 loc) · 1.29 KB
/
findPath.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
//
// main.cpp
// findPath
//
// Created by Sethupathi on 17/03/20.
// Copyright © 2020 Sethupathi. All rights reserved.
//
#include <iostream>
using namespace std;
void findPath(int maze[][20],int n, int x, int y, int path[][20])
{
if(x<0 || x> n-1 || y<0 || y> n-1)
{
return;
}
if(x==n-1 && y==n-1)
{
path[x][y]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<path[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
return;
}
if(maze[x][y]==0 || path[x][y]==1)
{
return;
}
path[x][y]=1;
findPath(maze, n, x, y+1, path);
findPath(maze, n, x, y-1, path);
findPath(maze, n, x+1, y, path);
findPath(maze, n, x-1, y, path);
//when you have explored a path, reset that specific item so that you do not explore the same path again.
path[x][y]=0;
}
void findPath(int maze[][20], int n)
{
int path[20][20] = {0};
findPath(maze, n, 0, 0, path);
}
int main(int argc, const char * argv[]) {
// insert code here...
// std::cout << "Hello, World!\n";
int n=3;
int maze[20][20] = {{1,1,0},{1,1,0},{0,1,1}};
findPath(maze, n);
return 0;
}