-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (141 loc) · 4.31 KB
/
main.cpp
File metadata and controls
165 lines (141 loc) · 4.31 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
#include <iostream>
#include <algorithm>
#include <ctime>
#include "Matrix.h"
#include "Board.h"
#include "functions.h"
using namespace std;
/*
vector<string> astar(Board* start)
{
// Create priority queue with the initial board
vector<Board*> pqueue;
pqueue.push_back(start);
do {
Board* top = pqueue.front(); // Get top board
if (top->getManhattanDist() == 0)
{
return top->getPath();
}
else
{
vector<Board*> children = top->getChildren();
for (vector<Board*>::iterator it = children.begin(); it != children.end(); it++)
{
pqueue.push_back(*it);
}
sort(pqueue.begin(), pqueue.end(), [](Board*& left, Board*& right) {
return (left->getEval() < right->getEval());
});
}
} while(!pqueue.empty());
}
*/
vector<string> astar(Board* start)
{
vector<Board*> open;
vector<Board*> closed;
open.push_back(start);
while (!open.empty())
{
Board* best = open.front();
//best->printBoard();
//cout << best->getEval() << endl;
int index = 0;
int indexToBeErased = 0;
for (vector<Board*>::iterator it = open.begin(); it != open.end(); it++)
{
if (best->getEval() > (*it)->getEval())
{
best = (*it);
indexToBeErased = index;
}
index++;
}
open.erase(open.begin() + indexToBeErased);
vector<Board*> children = best->getChildren();
for (vector<Board*>::iterator it = children.begin(); it != children.end(); it++)
{
if ((*it)->getManhattanDist() == 0) return (*it)->getPath();
bool checkOpen = true, checkClosed = true;
for (vector<Board*>::iterator jt = open.begin(); jt != open.end(); jt++)
{
if ((*it)->isEqualTo(*jt))
{
if ((*it)->getEval() > (*jt)->getEval())
{
checkOpen = false;
}
}
}
for (vector<Board*>::iterator jt = closed.begin(); jt != closed.end(); jt++)
{
if ((*it)->isEqualTo(*jt))
{
if ((*it)->getEval() > (*jt)->getEval())
{
checkClosed = false;
}
}
}
if (checkOpen && checkClosed) open.push_back(*it);
}
closed.push_back(best);
}
vector<string> thisShouldNotHappen;
thisShouldNotHappen.push_back("Couldn't do it :(");
return thisShouldNotHappen;
}
int main()
{
int n;
cout << "Enter n: "; // User is prompt to enter n
cin >> n; // User enters n
int size = sqrt(n + 1);
// User is prompt to enter matrix
cout << "Enter a matrix " << size << "x" << size << endl;
// User enters matrix
int temp, zeroX = -1, zeroY = -1;
Matrix start(size);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
cin >> temp;
start[i][j] = temp;
if (start[i][j] == 0)
{
zeroX = i;
zeroY = j;
}
}
}
if (zeroX == -1 || zeroY == -1)
{
cout << "Invalid board(no zero point). Please run again with a different board!" << endl;
return 0;
}
Board* initialBoard = new Board(start, zeroX, zeroY);
if (!initialBoard->isSolvable())
{
cout << "This board can not be solved. Please run again with a different board!" << endl;
return 0;
}
initialBoard->calcManhattanDist();
initialBoard->calcEval();
if (initialBoard->getEval() == 0)
{
cout << "This puzzle is already solved!" << endl;
return 0;
}
clock_t startTime, endTime;
startTime = clock();
vector<string> result = astar(initialBoard);
endTime = clock();
cout << "Number of steps: " << result.size() << endl;
for (vector<string>::iterator it = result.begin(); it != result.end(); it++)
{
cout << (*it) << " ";
}
cout << endl << "Time: " << (endTime - startTime) / (double) CLOCKS_PER_SEC << endl;
}