-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolver.cpp
49 lines (38 loc) · 1.21 KB
/
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
#include <iterator>
#include <algorithm>
#include <iostream>
#include "settings.h"
#include "solver.h"
SolveState solveBoard(Board b)
{
return solveBoardAll(b).front();
}
std::vector<SolveState> solveBoardAll(Board b)
{
int i, j;
std::vector<SolveState> solutions;
b.setStartLocations();
// initialize starting locations
for(i=0; i<BOARD_X; i++)
for(j=0; j<BOARD_Y; j++)
solutions.push_back(SolveState(b, i, j));
std::sort(solutions.begin(), solutions.end());
// go through all move lengths
for(unsigned int n_moves = 0; n_moves < MAX_MOVES; n_moves++) {
std::vector<SolveState> newSolutions;
// make a move on all states with the current highest move length
for(auto &iter : solutions)
if(iter.moves.size() == n_moves)
iter.addNewStates(newSolutions);
// add new states to solutions list
for(auto &iter : newSolutions)
solutions.push_back(iter);
// sort all solutions
std::sort(solutions.begin(), solutions.end());
// remove excess solutions
while(solutions.size() > NUM_SOLUTIONS) {
solutions.pop_back();
}
}
return solutions;
}