Coding
Coding
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
#define ROW 8
#define COL 5
struct cell {
// f = g + h
double f, g, h;
};
// is in range
return (row >= 0) && (row < ROW) && (col >= 0)
// blocked or not
if (grid[row][col] == 1)
return (true);
else
return (false);
return (true);
else
return (false);
return ((double)sqrt(
// to destination
stack<Pair> Path;
Path.push(make_pair(row, col));
row = temp_row;
col = temp_col;
Path.push(make_pair(row, col));
while (!Path.empty()) {
Path.pop();
return;
// to A* Search Algorithm
printf("Source is invalid\n");
return;
}
printf("Destination is invalid\n");
return;
== false) {
return;
== true) {
return;
bool closedList[ROW][COL];
memset(closedList, false, sizeof(closedList));
// of that cell
cell cellDetails[ROW][COL];
int i, j;
cellDetails[i][j].f = FLT_MAX;
cellDetails[i][j].g = FLT_MAX;
cellDetails[i][j].h = FLT_MAX;
cellDetails[i][j].parent_i = -1;
cellDetails[i][j].parent_j = -1;
i = src.first, j = src.second;
cellDetails[i][j].f = 0.0;
cellDetails[i][j].g = 0.0;
cellDetails[i][j].h = 0.0;
cellDetails[i][j].parent_i = i;
cellDetails[i][j].parent_j = j;
/*
Create an open list having information as-
where f = g + h,
pair.*/
set<pPair> openList;
// Put the starting cell on the open list and set its
// 'f' as 0
while (!openList.empty()) {
pPair p = *openList.begin();
openList.erase(openList.begin());
i = p.second.first;
j = p.second.second;
closedList[i][j] = true;
/*
N.W N N.E
\|/
\|/
W----Cell----E
/|\
/|\
S.W S S.E
if (isValid(i - 1, j) == true) {
// current successor
cellDetails[i - 1][j].parent_i = i;
cellDetails[i - 1][j].parent_j = j;
tracePath(cellDetails, dest);
foundDest = true;
return;
&& isUnBlocked(grid, i - 1, j)
== true) {
openList.insert(make_pair(
cellDetails[i - 1][j].parent_i = i;
cellDetails[i - 1][j].parent_j = j;
if (isValid(i + 1, j) == true) {
// current successor
cellDetails[i + 1][j].parent_j = j;
tracePath(cellDetails, dest);
foundDest = true;
return;
&& isUnBlocked(grid, i + 1, j)
== true) {
// OR
cellDetails[i + 1][j].parent_i = i;
cellDetails[i + 1][j].parent_j = j;
if (isValid(i, j + 1) == true) {
// current successor
cellDetails[i][j + 1].parent_i = i;
cellDetails[i][j + 1].parent_j = j;
tracePath(cellDetails, dest);
foundDest = true;
return;
}
// If the successor is already on the closed
&& isUnBlocked(grid, i, j + 1)
== true) {
// OR
openList.insert(make_pair(
cellDetails[i][j + 1].parent_i = i;
cellDetails[i][j + 1].parent_j = j;
if (isValid(i, j - 1) == true) {
// current successor
cellDetails[i][j - 1].parent_i = i;
cellDetails[i][j - 1].parent_j = j;
tracePath(cellDetails, dest);
foundDest = true;
return;
== true) {
// OR
openList.insert(make_pair(
cellDetails[i][j - 1].parent_i = i;
cellDetails[i][j - 1].parent_j = j;
}
}
//------------
if (isValid(i - 1, j + 1) == true) {
// current successor
tracePath(cellDetails, dest);
foundDest = true;
return;
&& isUnBlocked(grid, i - 1, j + 1)
== true) {
// OR
openList.insert(make_pair(
//------------
// Only process this cell if this is a valid one
if (isValid(i - 1, j - 1) == true) {
// current successor
tracePath(cellDetails, dest);
foundDest = true;
return;
&& isUnBlocked(grid, i - 1, j - 1)
== true) {
// OR
openList.insert(make_pair(
//------------
if (isValid(i + 1, j + 1) == true) {
// current successor
if (isDestination(i + 1, j + 1, dest) == true) {
tracePath(cellDetails, dest);
foundDest = true;
return;
&& isUnBlocked(grid, i + 1, j + 1)
== true) {
// OR
openList.insert(make_pair(
//------------
if (isValid(i + 1, j - 1) == true) {
// current successor
tracePath(cellDetails, dest);
foundDest = true;
return;
&& isUnBlocked(grid, i + 1, j - 1)
== true) {
// OR
openList.insert(make_pair(
fNew, make_pair(i + 1, j - 1)));
// blockages)
if (foundDest == false)
return;
int main()
{
/* Description of the Grid-
int grid[ROW][COL]
= { { 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1 },
{ 1, 1, 1, 0, 1 },
{ 1, 0, 1, 0, 1 },
{ 1, 0, 1, 1, 1 },
{ 1, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 1 } };
return (0);