-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
112 lines (100 loc) · 3.98 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
#define N 10 // Column size of maze
#define M 10 // Row size of maze
int n = N , m = M; // maze of n*m matrix
int ix=0,iy=0; // initial position of rat
int fx=9,fy=0; // coordinates of food
bool visited[N][M]; // Matrix to represent visited coordinates
class node // Node class to represent coordinates
{
public:
int x, y;
node(int i=0, int j=0) //Constructor
{
x = i;
y = j;
}
};
void printstack(stack<node> s) // Function to print contents of stack
{
if (s.empty()) // If stack is empty then return
return;
node c = s.top();
s.pop(); // Pop the top element of the stack
printstack(s); // Recursively call the function PrintStack
cout<<"("<<c.x<<","<<c.y<<")"<<endl;
s.push(c); // Push the same element onto the stack to preserve the order
}
bool isReachable(int maze[N][M]) // Function to find path in maze
{
int i = ix, j = iy; // Initial position of rat in maze
node temp(i, j); // Create an object temp
stack<node> s; // Create stack s of type node
s.push(temp); // Push node temp to stack
while (!s.empty())
{
temp=s.top();
i=temp.x,j=temp.y; // Stores current position of rat
if(i==fx && j==fy) // Checks if current position is equal to food coordinates
{
printstack(s); // Prints the contents of stack
return true;
}
if(j+1<m && visited[i][j+1] && maze[i][j+1]) //RIGHT
{
node temp1(i,j+1); // creates an object with coordinates of node in the right
s.push(temp1); // Stores the coordinates in stack
visited[i][j+1]=false; // Stores false to indicate coordinates is visited
}
else if(i+1<n && visited[i+1][j] && maze[i+1][j]) //DOWN
{
node temp1(i+1,j); // creates an object with coordinates of node below
s.push(temp1); // Stores the coordinates in stack
visited[i+1][j]=false; // Stores false to indicate coordinates is visited
}
else if(i-1>=0 && visited[i-1][j] && maze[i-1][j]) //UP
{
node temp1(i-1,j); // creates an object with coordinates of node above
s.push(temp1); // Stores the coordinates in stack
visited[i-1][j]=false; // Stores false to indicate coordinates is visited
}
else if(j-1>=0 && visited[i][j-1] && maze[i][j-1]) //LEFT
{
node temp1(i,j-1); // creates an object with coordinates of node in the left
s.push(temp1); // Stores the coordinates in stack
visited[i][j-1]=false; // Stores false to indicate coordinates is visited
}
else // If there is no path, rat goes to previous coordinates
{
visited[temp.x][temp.y]=false;
s.pop();
}
}
return false; // If the stack is empty and no path is found return false.
}
int main() //Main function
{
// Initially setting the visited array to true (unvisited)
memset(visited, true, sizeof(visited));
// Maze matrix
int maze[N][M] = {
{1, 1, 0, 0, 1, 0, 0, 1, 0, 1},
{0, 1, 0, 1, 1, 1, 1, 0, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 1, 1, 1},
{0, 1, 0, 0, 1, 0, 1, 0, 1, 0},
{1, 1, 1, 1, 1, 0, 0, 0, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 0, 1},
{0, 0, 1, 1, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 0, 0, 0, 1, 1},
};
if (isReachable(maze)) // Checks if path is available
cout << "Path Found!" << '\n';
else // If path is not available
cout << "No Path Found!" << '\n';
return 0;
}