-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearcherSolver.h
More file actions
82 lines (69 loc) · 2.67 KB
/
SearcherSolver.h
File metadata and controls
82 lines (69 loc) · 2.67 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
//
// Created by duni on 21/01/2020.
//
#ifndef PROBLEMSOLVER_SEARCHERSOLVER_H
#define PROBLEMSOLVER_SEARCHERSOLVER_H
#include "Solver.h"
#include "AlgorithmOptions.h"
#include "Point.h"
#include <string>
#include <iostream>
template<class P>
class SearcherSolver : public Solver<Searchable<P>, string> {
private:
AlgorithmOptions<P> *ao;
public:
// This is a constructor function
SearcherSolver(AlgorithmOptions<P> *ao) {
this->ao = ao;
}
// This function activates the suitable algorithm for the given problem
string solve(Searchable<P> *problem) {
Searcher<P> *searcher = ao->getAlgorithm();
vector<State<Point> *> vec = searcher->search(problem);
// Send the solution to be modified into the wanted solution format
string solution = solutionFormat(vec);
return solution;
}
// This function returns the algorithm name that solved the problem
string getName() {
return ao->getAlgorithmName();
}
// This function returns the given solution in the right format to print it with
string solutionFormat(vector<State<Point> *> solution) {
string strSolution = "";
string direction;
string strToSend = "";
basic_string<char> cost;
int x;
int y;
auto it = solution.begin();
// Go through the entire solution
for (it = solution.begin(); it != solution.end() - 1; it++) {
// Get the next node because the comparison is always done between the current node and the next one
auto next = it + 1;
// Calculate the node which is located beneath the current one
if ((*it)->getState()->getX() + 1 == (*next)->getState()->getX()) {
strSolution += "Down";
}
// Calculate the node which is located above the current one
if ((*it)->getState()->getX() - 1 == (*next)->getState()->getX()) {
strSolution += "Up";
}
// Calculate the node which is located to right of the current one
if ((*it)->getState()->getY() + 1 == (*next)->getState()->getY()) {
strSolution += "Right";
}
// Calculate the node which is located to left of the current one
if ((*it)->getState()->getY() - 1 == (*next)->getState()->getY()) {
strSolution += "Left";
}
cost = to_string((*next)->getCumulativeCost());
strSolution += " ";
strSolution += "(" + cost + ")" + ", ";
}
strToSend = strSolution.substr(0, strSolution.size() - 2);
return strToSend;
}
};
#endif //PROBLEMSOLVER_SEARCHERSOLVER_H