Skip to content

Commit e605612

Browse files
committed
Initial commit
0 parents  commit e605612

10 files changed

+676
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Shortest-Path-Algo

Summary.docx

22.9 KB
Binary file not shown.

astar.cpp

+355
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
#include<bits/stdc++.h>
2+
#define ROW 9
3+
#define COL 10
4+
using namespace std;
5+
typedef pair<int, int> Pair;
6+
typedef pair<double, pair<int, int> > pPair;
7+
int arr[ROW][COL]={0};
8+
struct cell{
9+
int parent_i, parent_j;
10+
double f, g, h;
11+
};
12+
bool isValid(int row, int col){
13+
return (row >= 0) && (row < ROW) &&
14+
(col >= 0) && (col < COL);
15+
}
16+
bool isUnBlocked(int grid[][COL], int row, int col){
17+
if (grid[row][col] == 1)
18+
return (true);
19+
else
20+
return (false);
21+
}
22+
bool isDestination(int row, int col, Pair dest){
23+
if (row == dest.first && col == dest.second)
24+
return (true);
25+
else
26+
return (false);
27+
}
28+
double calculateHValue(int row, int col, Pair dest){
29+
return ((double)sqrt ((row-dest.first)*(row-dest.first)
30+
+ (col-dest.second)*(col-dest.second)));
31+
}
32+
void tracePath(cell cellDetails[][COL], Pair dest){
33+
printf ("\nThe Path is ");
34+
int row = dest.first;
35+
int col = dest.second;
36+
stack<Pair> Path;
37+
while (!(cellDetails[row][col].parent_i == row
38+
&& cellDetails[row][col].parent_j == col ))
39+
{
40+
Path.push (make_pair (row, col));
41+
int temp_row = cellDetails[row][col].parent_i;
42+
int temp_col = cellDetails[row][col].parent_j;
43+
row = temp_row;
44+
col = temp_col;
45+
}
46+
Path.push (make_pair (row, col));
47+
while (!Path.empty()){
48+
pair<int,int> p = Path.top();
49+
Path.pop();
50+
printf("-> (%d,%d) ",p.first,p.second);
51+
arr[p.first][p.second]=99;
52+
}cout<<endl;
53+
return;
54+
}
55+
void aStarSearch(int grid[][COL], Pair src, Pair dest){
56+
if (isValid (src.first, src.second) == false){
57+
printf ("Source is invalid\n");
58+
return;
59+
}
60+
if (isValid (dest.first, dest.second) == false){
61+
printf ("Destination is invalid\n");
62+
return;
63+
}
64+
if (isUnBlocked(grid, src.first, src.second) == false ||
65+
isUnBlocked(grid, dest.first, dest.second) == false)
66+
{
67+
printf ("Source or the destination is blocked\n");
68+
return;
69+
}
70+
if (isDestination(src.first, src.second, dest) == true){
71+
printf ("We are already at the destination\n");
72+
return;
73+
}
74+
bool closedList[ROW][COL];
75+
memset(closedList, false, sizeof (closedList));
76+
cell cellDetails[ROW][COL];
77+
int i, j;
78+
for (i=0; i<ROW; i++){
79+
for (j=0; j<COL; j++){
80+
cellDetails[i][j].f = FLT_MAX;
81+
cellDetails[i][j].g = FLT_MAX;
82+
cellDetails[i][j].h = FLT_MAX;
83+
cellDetails[i][j].parent_i = -1;
84+
cellDetails[i][j].parent_j = -1;
85+
}
86+
}
87+
i = src.first, j = src.second;
88+
cellDetails[i][j].f = 0.0;
89+
cellDetails[i][j].g = 0.0;
90+
cellDetails[i][j].h = 0.0;
91+
cellDetails[i][j].parent_i = i;
92+
cellDetails[i][j].parent_j = j;
93+
94+
set<pPair> openList;
95+
openList.insert(make_pair (0.0, make_pair (i, j)));
96+
bool foundDest = false;
97+
while (!openList.empty()){
98+
pPair p = *openList.begin();
99+
openList.erase(openList.begin());
100+
i = p.second.first;
101+
j = p.second.second;
102+
closedList[i][j] = true;
103+
104+
double gNew, hNew, fNew;
105+
if (isValid(i-1, j) == true){
106+
if (isDestination(i-1, j, dest) == true){
107+
cellDetails[i-1][j].parent_i = i;
108+
cellDetails[i-1][j].parent_j = j;
109+
tracePath (cellDetails, dest);
110+
foundDest = true;
111+
return;
112+
}
113+
else if (closedList[i-1][j] == false &&
114+
isUnBlocked(grid, i-1, j) == true)
115+
{
116+
gNew = cellDetails[i][j].g + 1.0;
117+
hNew = calculateHValue (i-1, j, dest);
118+
fNew = gNew + hNew;
119+
if (cellDetails[i-1][j].f == FLT_MAX ||
120+
cellDetails[i-1][j].f > fNew)
121+
{
122+
openList.insert( make_pair(fNew,
123+
make_pair(i-1, j)));
124+
cellDetails[i-1][j].f = fNew;
125+
cellDetails[i-1][j].g = gNew;
126+
cellDetails[i-1][j].h = hNew;
127+
cellDetails[i-1][j].parent_i = i;
128+
cellDetails[i-1][j].parent_j = j;
129+
}
130+
}
131+
}
132+
if (isValid(i+1, j) == true){
133+
if (isDestination(i+1, j, dest) == true){
134+
cellDetails[i+1][j].parent_i = i;
135+
cellDetails[i+1][j].parent_j = j;
136+
tracePath(cellDetails, dest);
137+
foundDest = true;
138+
return;
139+
}
140+
else if (closedList[i+1][j] == false &&
141+
isUnBlocked(grid, i+1, j) == true)
142+
{
143+
gNew = cellDetails[i][j].g + 1.0;
144+
hNew = calculateHValue(i+1, j, dest);
145+
fNew = gNew + hNew;
146+
if (cellDetails[i+1][j].f == FLT_MAX ||
147+
cellDetails[i+1][j].f > fNew)
148+
{
149+
openList.insert( make_pair (fNew, make_pair (i+1, j)));
150+
cellDetails[i+1][j].f = fNew;
151+
cellDetails[i+1][j].g = gNew;
152+
cellDetails[i+1][j].h = hNew;
153+
cellDetails[i+1][j].parent_i = i;
154+
cellDetails[i+1][j].parent_j = j;
155+
}
156+
}
157+
}
158+
if (isValid (i, j+1) == true){
159+
if (isDestination(i, j+1, dest) == true){
160+
cellDetails[i][j+1].parent_i = i;
161+
cellDetails[i][j+1].parent_j = j;
162+
tracePath(cellDetails, dest);
163+
foundDest = true;
164+
return;
165+
}
166+
else if (closedList[i][j+1] == false &&
167+
isUnBlocked (grid, i, j+1) == true)
168+
{
169+
gNew = cellDetails[i][j].g + 1.0;
170+
hNew = calculateHValue (i, j+1, dest);
171+
fNew = gNew + hNew;
172+
if (cellDetails[i][j+1].f == FLT_MAX ||
173+
cellDetails[i][j+1].f > fNew)
174+
{
175+
openList.insert( make_pair(fNew,
176+
make_pair (i, j+1)));
177+
cellDetails[i][j+1].f = fNew;
178+
cellDetails[i][j+1].g = gNew;
179+
cellDetails[i][j+1].h = hNew;
180+
cellDetails[i][j+1].parent_i = i;
181+
cellDetails[i][j+1].parent_j = j;
182+
}
183+
}
184+
}
185+
if (isValid(i, j-1) == true){
186+
if (isDestination(i, j-1, dest) == true){
187+
cellDetails[i][j-1].parent_i = i;
188+
cellDetails[i][j-1].parent_j = j;
189+
tracePath(cellDetails, dest);
190+
foundDest = true;
191+
return;
192+
}
193+
else if (closedList[i][j-1] == false &&
194+
isUnBlocked(grid, i, j-1) == true)
195+
{
196+
gNew = cellDetails[i][j].g + 1.0;
197+
hNew = calculateHValue(i, j-1, dest);
198+
fNew = gNew + hNew;
199+
if (cellDetails[i][j-1].f == FLT_MAX ||
200+
cellDetails[i][j-1].f > fNew)
201+
{
202+
openList.insert( make_pair (fNew,
203+
make_pair (i, j-1)));
204+
cellDetails[i][j-1].f = fNew;
205+
cellDetails[i][j-1].g = gNew;
206+
cellDetails[i][j-1].h = hNew;
207+
cellDetails[i][j-1].parent_i = i;
208+
cellDetails[i][j-1].parent_j = j;
209+
}
210+
}
211+
}
212+
if (isValid(i-1, j+1) == true){
213+
if (isDestination(i-1, j+1, dest) == true){
214+
cellDetails[i-1][j+1].parent_i = i;
215+
cellDetails[i-1][j+1].parent_j = j;
216+
tracePath (cellDetails, dest);
217+
foundDest = true;
218+
return;
219+
}
220+
else if (closedList[i-1][j+1] == false &&
221+
isUnBlocked(grid, i-1, j+1) == true)
222+
{
223+
gNew = cellDetails[i][j].g + 1.414;
224+
hNew = calculateHValue(i-1, j+1, dest);
225+
fNew = gNew + hNew;
226+
if (cellDetails[i-1][j+1].f == FLT_MAX ||
227+
cellDetails[i-1][j+1].f > fNew)
228+
{
229+
openList.insert( make_pair (fNew,
230+
make_pair(i-1, j+1)));
231+
cellDetails[i-1][j+1].f = fNew;
232+
cellDetails[i-1][j+1].g = gNew;
233+
cellDetails[i-1][j+1].h = hNew;
234+
cellDetails[i-1][j+1].parent_i = i;
235+
cellDetails[i-1][j+1].parent_j = j;
236+
}
237+
}
238+
}
239+
if (isValid (i-1, j-1) == true){
240+
if (isDestination (i-1, j-1, dest) == true){
241+
cellDetails[i-1][j-1].parent_i = i;
242+
cellDetails[i-1][j-1].parent_j = j;
243+
tracePath (cellDetails, dest);
244+
foundDest = true;
245+
return;
246+
}
247+
else if (closedList[i-1][j-1] == false &&
248+
isUnBlocked(grid, i-1, j-1) == true)
249+
{
250+
gNew = cellDetails[i][j].g + 1.414;
251+
hNew = calculateHValue(i-1, j-1, dest);
252+
fNew = gNew + hNew;
253+
if (cellDetails[i-1][j-1].f == FLT_MAX ||
254+
cellDetails[i-1][j-1].f > fNew)
255+
{
256+
openList.insert( make_pair (fNew, make_pair (i-1, j-1)));
257+
cellDetails[i-1][j-1].f = fNew;
258+
cellDetails[i-1][j-1].g = gNew;
259+
cellDetails[i-1][j-1].h = hNew;
260+
cellDetails[i-1][j-1].parent_i = i;
261+
cellDetails[i-1][j-1].parent_j = j;
262+
}
263+
}
264+
}
265+
if (isValid(i+1, j+1) == true){
266+
if (isDestination(i+1, j+1, dest) == true){
267+
cellDetails[i+1][j+1].parent_i = i;
268+
cellDetails[i+1][j+1].parent_j = j;
269+
tracePath (cellDetails, dest);
270+
foundDest = true;
271+
return;
272+
}
273+
else if (closedList[i+1][j+1] == false &&
274+
isUnBlocked(grid, i+1, j+1) == true)
275+
{
276+
gNew = cellDetails[i][j].g + 1.414;
277+
hNew = calculateHValue(i+1, j+1, dest);
278+
fNew = gNew + hNew;
279+
if (cellDetails[i+1][j+1].f == FLT_MAX ||
280+
cellDetails[i+1][j+1].f > fNew)
281+
{
282+
openList.insert(make_pair(fNew,
283+
make_pair (i+1, j+1)));
284+
cellDetails[i+1][j+1].f = fNew;
285+
cellDetails[i+1][j+1].g = gNew;
286+
cellDetails[i+1][j+1].h = hNew;
287+
cellDetails[i+1][j+1].parent_i = i;
288+
cellDetails[i+1][j+1].parent_j = j;
289+
}
290+
}
291+
}
292+
if (isValid (i+1, j-1) == true)
293+
{
294+
if (isDestination(i+1, j-1, dest) == true)
295+
{
296+
cellDetails[i+1][j-1].parent_i = i;
297+
cellDetails[i+1][j-1].parent_j = j;
298+
tracePath(cellDetails, dest);
299+
foundDest = true;
300+
return;
301+
}
302+
else if (closedList[i+1][j-1] == false &&
303+
isUnBlocked(grid, i+1, j-1) == true)
304+
{
305+
gNew = cellDetails[i][j].g + 1.414;
306+
hNew = calculateHValue(i+1, j-1, dest);
307+
fNew = gNew + hNew;
308+
if (cellDetails[i+1][j-1].f == FLT_MAX ||
309+
cellDetails[i+1][j-1].f > fNew)
310+
{
311+
openList.insert(make_pair(fNew,
312+
make_pair(i+1, j-1)));
313+
cellDetails[i+1][j-1].f = fNew;
314+
cellDetails[i+1][j-1].g = gNew;
315+
cellDetails[i+1][j-1].h = hNew;
316+
cellDetails[i+1][j-1].parent_i = i;
317+
cellDetails[i+1][j-1].parent_j = j;
318+
}
319+
}
320+
}
321+
}
322+
if (foundDest == false)
323+
printf("Failed to find the Destination Cell\n");
324+
return;
325+
}
326+
int main(){
327+
int grid[ROW][COL];
328+
ifstream myfile;
329+
myfile.open ("input.txt");
330+
cout<<"\n\tInput Maze\n";
331+
for(int i = 0; i < ROW; i++)
332+
for(int j = 0; j < COL; j++)
333+
myfile >> grid[i][j];
334+
335+
for(int i = 0; i < ROW; i++){
336+
cout<<"\t";
337+
for(int j = 0; j < COL; j++)
338+
cout << grid[i][j]<<" ";
339+
cout<<endl;
340+
}
341+
Pair src = make_pair(8, 0);
342+
Pair dest = make_pair(0, 9);
343+
cout<<"\n\tSource (8, 0) Destination (0, 9)\n";
344+
aStarSearch(grid, src, dest);
345+
for(int i = 0; i < ROW; i++){
346+
cout<<"\t";
347+
for(int j = 0; j < COL; j++){
348+
if (arr[i][j]!=0)
349+
cout << "*"<<" ";
350+
else
351+
cout << grid[i][j]<<" ";
352+
}cout<<endl;
353+
}
354+
return(0);
355+
}

0 commit comments

Comments
 (0)