-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktracking-RatInMaze.cpp
More file actions
218 lines (202 loc) · 8.19 KB
/
Backtracking-RatInMaze.cpp
File metadata and controls
218 lines (202 loc) · 8.19 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*---------------------------------------------------------------
---------- :: RAT IN MAZE :: ------------
---------- :: BY :: ------------
--------- :: Amna Azam :: ------------
-----------------------------------------------------------------
*//*
---------------------------------------------------------------
----------- :: REQUIREMENT :: ---------------------
------------------------------------------------------
=> Take size of maze, source cell, destination cell and 2D binary maze input from file.
=> Reach the mouce from source Cell to destination Cell
=> 0- open path
=> 1- blocked path
=> Rat can move in 4 directions(up, left, right, down).
=> Print the path cells and also show these cells with stars(*) on console and in output file as well.
*//*
------------------------------------------------------
----------- :: LOGIC :: ------------
------------------------------------------------------
=> Firstly, Make a cell on which mouce exists and push that cell in stack everytime.
=> Mouce will follow those cells containing 0.
=> Use bool unvisited array of maze's size to keep the track either we have visited any cell or not.
=> Any cell visited by mouce must be marked visited so that mouce can not visit, visited cell again.
=> When we get dead cell(the cell where no path exists for mouce to move in any direction but only back),
then pop the cells from stack for backtracking(to reach at previous cell).
=> At each cell, check all the four possiblities for mouce to move on and push each visited current cell in stack.
=> At the end if stack is empty, then no any path found to reach at destination.
=> Stack will contain any one path to reach the mouce at destination.
*//*-----------------------------------------------------
------- :: C++ IMPLEMENTATION :: ------
------------------------------------------------------*/
//-----------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include<fstream>
#include <stack>
using namespace std;
//--------------------------------------------------------------------------------------------
//To pop a cell from stack, we need explicitly pop function in class myStack
//inherited from built-in stack.
template<class T>
class myStack : public stack<T>
{
public:
T pop() {
T poped = stack<T>::top();
stack<T>::pop();
return poped;
}
};
//-----------------------------------------------------------------------------------------------
class Cell {
public:
int x, y;
Cell(int x, int y) //Make a cell
{
this->x = x;
this->y = y;
}
bool operator == (const Cell& c) const //check either two cells are equal
{
return x == c.x && y == c.y;
}
};
//-----------------------------------------------------------------------------------------
//does mouce reach at destination?
bool isDestination(fstream& f,char **maze, int size, int srcX, int srcY, int desX, int desY)
{
//----------------------- make array to keep track for visited cells
bool** unVisited = new bool* [size];
for (int i = 0; i < size; i++)
unVisited[i] = new bool[size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
unVisited[i][j] = true; //Assume all cells are unvisited.
//----------------------------------------------
myStack<Cell> s;
Cell currCell(srcX, srcY); //start move from source cell -------- Make source cell your current cell.
Cell destCell(desX, desY); //make destination cell
s.push(currCell); //push current cell in stack
unVisited[srcX][srcY] = false; //Mark the source cell visited.
while (!s.empty())
{
currCell = s.top();
if (currCell == destCell) //Are we at destination cell?
{
myStack<Cell> s1; //Use this stack to print the cells from source to destination noy destination to cell
while(!s.empty())
s1.push(s.pop());
while(!s1.empty())
{
Cell n = s1.pop();
maze[n.x][n.y] = '*'; //Mark the correct path with stars in maze
cout << "(" << n.x << "," << n.y << ")"; //print on console
f << "(" << n.x << "," << n.y << ")"; //print in output file
}
return true; //path founded
}
/* Checking the right direction
-------CHECK?
Is the right cell in our bounded limit &&
[is it open(not conating wall i.e. 1) || is it our destination 'c'] &&
is it invisited?*/
if (currCell.y + 1 < size && (maze[currCell.x][currCell.y + 1] == '0' || maze[currCell.x][currCell.y + 1] =='c') && unVisited[currCell.x][currCell.y + 1])
{
Cell newCell(currCell.x, currCell.y + 1);
s.push(newCell);
unVisited[currCell.x][currCell.y + 1] = false; //visited
}
/* Checking the down direction
-------CHECK?
Is the down cell in our bounded limit &&
[is it open(not conating wall i.e. 1) || is it our destination 'c'] &&
is it unvisited?*/
else if (currCell.x + 1 < size && (maze[currCell.x + 1][currCell.y] == '0' || maze[currCell.x + 1][currCell.y] == 'c') && unVisited[currCell.x + 1][currCell.y])
{
Cell newCell(currCell.x + 1, currCell.y);
s.push(newCell);
unVisited[currCell.x + 1][currCell.y] = false; //visited
}
/* Checking the up direction
-------CHECK?
Is the up cell in our bounded limit &&
[is it open(not conating wall i.e. 1) || is it our destination 'c'] &&
is it unvisited?*/
else if (currCell.x - 1 >= 0 && (maze[currCell.x - 1][currCell.y] == '0' || maze[currCell.x - 1][currCell.y] == 'c') && unVisited[currCell.x - 1][currCell.y])
{
Cell newCell(currCell.x - 1, currCell.y);
s.push(newCell);
unVisited[currCell.x - 1][currCell.y] = false; //visited
}
/* Checking the left direction
-------CHECK?
Is the left cell in our bounded limit &&
[is it open(not conating wall i.e. 1) || is it our destination 'c'] &&
is it unvisited?*/
else if (currCell.y - 1 >= 0 && (maze[currCell.x][currCell.y - 1] == '0' || maze[currCell.x][currCell.y - 1] == 'c') && unVisited[currCell.x][currCell.y - 1])
{
Cell newCell(currCell.x, currCell.y - 1);
s.push(newCell);
unVisited[currCell.x][currCell.y - 1] = false; //visited
}
//oops we were at dead cell
else
s.pop(); //backtracking
}
return false; //no path found
}
// Driver code
int main()
{
int size;
ifstream fin; // declare stream variable name
fin.open("in.txt", ios::in); // open file
if (fin.fail()) //does file exists?
cout << "Input File does not exist...\n";
else
{
fin >> size; //read size of maze from file
cout << size << "\n"; //print size on console
int sx, sy;
fin >> sx; fin >> sy; //read source cell from file
cout << sx << " " << sy << "\n"; //print on console
int dx, dy;
fin >> dx; fin >> dy; //Read destination cell from file
cout << dx << " " << dy << "\n"; //print on console
char** maze = new char* [size]; //make a 2d maze
for (int i = 0; i < size; i++)
maze[i] = new char[size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) //read maze from file
fin >> maze[i][j];
fin.close();
fstream fout; // declare stream variable name for output file
fout.open("out.txt", ios::out); //open a file
fout << size << "\n";
fout << sx << " " << sy << "\n"; //print source cell on file
fout << dx << " " << dy << "\n"; //print destination cell on file
if (isDestination(fout, maze, size, sx, sy, dx, dy))
{
cout << "\n\n";
fout << "\n\n";
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
cout << maze[i][j] << " "; //print maze on console
fout << maze[i][j] << " "; //print maze on file
}
cout << endl;
fout << endl;
}
}
else
{
cout << "No Path Found!" << '\n';
fout << "No Path Found!" << '\n';
}
fout.close();
}
cout << "\n\nProgram ended successfully....";
return 0;
}