-
Notifications
You must be signed in to change notification settings - Fork 0
/
Astar.cpp
167 lines (155 loc) · 4.31 KB
/
Astar.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
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
#include <bits/stdc++.h>
using namespace std;
#define DIMENSION 3 // Size of board 3X3 formalized to avoid magic numbers
class Node {
public:
vector<vector<int>> state;
Node* parent;
int cost;
int heuristic;
Node* next;
Node* child;
Node(vector<vector<int>> puzzleState, int pathCost, int heuristicValue, Node* parentNode) {
this->cost = pathCost;
this->heuristic = heuristicValue;
this->state = puzzleState;
this->parent = parentNode;
next = NULL;
child = NULL;
}
};
void printBoard(vector<vector<int>> board, int size)
{
for(int i = 0; i<size;++i)
{
for(int j = 0; j<size;++j)
{
cout<<board[i][j]<<' ';
}
cout<<'\n';
}
}
vector<vector<int>> target = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}};
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
Node* OPEN = NULL;
Node* CLOSED = NULL;
int calculateHeuristic(vector<vector<int>> puzzle) {
int h = 0;
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
h += abs(target[i][j] - puzzle[i][j]);
}
}
return static_cast<int>(pow(h, 0.5));
}
void push(Node* head) {
if (OPEN == NULL) {
OPEN = head;
return;
}
int priority = head->heuristic;
Node* start = OPEN;
while (start->next != NULL && start->next->heuristic < priority) {
start = start->next;
}
head->next = start->next;
start->next = head;
}
void pushClosed(Node* head){
if (CLOSED == NULL) {
CLOSED = head;
return;
}
int priority = head->heuristic;
Node* start = CLOSED;
while (start->next != NULL && start->next->heuristic < priority) {
start = start->next;
}
head->next = start->next;
start->next = head;
}
void getNeighbors(Node* node) {
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
if (node->state[i][j] == 0) {
for (int k = 0; k < 4; k++) {
if (0 <= i + dx[k] && i + dx[k] < 3 && 0 <= j + dy[k] && j + dy[k] < 3) {
vector<vector<int>> newState = node->state;
swap(newState[i][j], newState[i + dx[k]][j + dy[k]]);
push(new Node(newState, node->cost + 1, calculateHeuristic(newState), node));
}
}
}
}
}
}
void removeNode(Node* node) {
if (OPEN == NULL) {
return;
}
if (OPEN == node) {
OPEN = OPEN->next;
return;
}
Node* temp = OPEN;
while (temp->next != NULL && temp->next != node) {
temp = temp->next;
}
if (temp->next == NULL) {
return;
}
temp->next = temp->next->next;
}
Node* aStar(Node* start) {
push(start);
while (OPEN != NULL) {
Node* current = OPEN;
OPEN = OPEN->next;
if (current->state == target) {
return current;
}
pushClosed(current);
getNeighbors(current);
removeNode(current);
}
return nullptr;
}
int main() {
vector<vector<int>> initialPuzzle(DIMENSION, vector<int>(DIMENSION, 0));
cout << "Enter initial 8-puzzle position:" << endl;
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
cin >> initialPuzzle[i][j];
}
}
cout << "Initial state of puzzle is:" << endl;
printBoard(initialPuzzle,DIMENSION);
Node* startNode = new Node(initialPuzzle, 0, calculateHeuristic(initialPuzzle), NULL);
Node* goalNode = aStar(startNode);
cout << "The Goal state has been reached with cost of " << goalNode->cost << endl;
cout << "All the OPEN iterations look like" << endl;
Node* iter = OPEN;
cout << "OPEN" << "->";
while (iter != NULL) {
cout << iter->heuristic << "->";
iter = iter->next;
}
cout << "NULL" << endl;
cout << "All the CLOSED iterations look like" << endl;
iter = CLOSED;
cout << "CLOSED" << "->";
while (iter != NULL) {
cout << iter->heuristic << "->";
iter = iter->next;
}
cout << "NULL" << endl;
cout << "Thus solution is - "<<'\n';
iter = goalNode;
while(iter!=NULL){
printBoard(iter->state,DIMENSION);
cout<<"\n";
iter = iter->parent;
}
cout<<"Initial State\n";
}