-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_solver.cpp
112 lines (90 loc) · 2.87 KB
/
puzzle_solver.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
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include "puzzle_solver.h"
using namespace std;
PuzzleSolver::PuzzleSolver(const Board &b) : b_(b)
{
//b_ = b;
expansions_ = 0;
}
PuzzleSolver::~PuzzleSolver()
{
// deallocate mylist and boardset
//for(BoardSet::iterator it=BoardSet.begin();it!=slist_.end();++it)
//std::cout << "puzzlesolver destructor" << std::endl;
// deallocating Boardset which has data of dynamically allocated Boards
//cout << "closeList size: " << closeList.size() << endl;
//for(BoardSet::iterator it=closeList.begin();it!=closeList.end();++it){
// delete (*it);
//}
//cout << "openList size: " << openList.size() << endl;
//cout << "closeList size: " << closeList.size() << endl;
int size = garbage.size();
//cout << "garbage size: " << size << endl;
for(int i=0;i<size;++i){
delete garbage[0];
garbage.pop(0);
}
//cout << "garvage size: " << garbage.size() << endl;
}
int PuzzleSolver::run(PuzzleHeuristic *ph)
//int PuzzleSolver::run()
{
PMMinList openList;
BoardSet closeList;
map<int,Board*> BoardMap;
int num_moves = 0;
PuzzleMove *firstMove = new PuzzleMove(b_);
openList.push(firstMove);
closeList.insert(firstMove->b_);
while(openList.empty() != true){
PuzzleMove *move = openList.top();
openList.pop();
//closeList.insert(move->b_); // save board to closeList
garbage.push_back(move); // save Puzzlemove to garbage list
if(move->b_->solved() == true){
// Trace path back to start
PuzzleMove *temp = move;
while(temp->prev_ != NULL){
// cout << "g_ : " << temp->g_ << endl;
trace.push_back(temp->tileMove_);
temp = temp->prev_;
// cout << "1" << endl;
}
//cout << "openList size: " << openList.size() << endl;
//cout << "closeList size: " << closeList.size() << endl;
//cout << "garbage size: " << garbage.size() << endl;
//cout << "num_moves: " << num_moves << endl;
return num_moves;
//break;
}
BoardMap = move->b_->potentialMoves(); // Dynamically allocated Boards will be deleted in garbage destructor
for(std::map<int,Board*>::iterator it=BoardMap.begin();it!=BoardMap.end();++it){
if(closeList.find(it->second) == closeList.end()){
PuzzleMove *temp = new PuzzleMove(it->first,it->second,move); // this will be deleted in the openList
temp->h_ = ph->compute(it->second->getTiles(), it->second->getSize()); // computing h score
//garbage.push_back(temp);
closeList.insert(it->second);
openList.push(temp);
expansions_ ++;
}
else
delete it->second;
//cout << "2" << endl;
}
//BoardMap.clear();
num_moves ++;
//cout << "3" << endl;
}
return -1;
}
deque<int> PuzzleSolver::get_solution()
{
return trace;
}
int PuzzleSolver::getNumExpansions()
{
return expansions_;
}