Repository navigation
Expand file tree
/
Copy pathBFS.h
More file actions
61 lines (50 loc) · 1.42 KB
/
Copy pathBFS.h
File metadata and controls
61 lines (50 loc) · 1.42 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
//
// Created by ori on 14/01/2020.
//
#ifndef EX4_BFS_H
#define EX4_BFS_H
#include <string>
#include "Searchable.h"
#include "Searcher.h"
#include "MySearcher.h"
#include <queue>
#include <iterator>
template<typename T>
class BFS : public MySearcher<T> {
private:
queue<State<T> *> myQueue;
vector<State<T> *> myVec;
public:
virtual string search(Searchable<T> *mySearchable) override {
State<T> *node = mySearchable->getInitialState();
node->setColor('g');
myQueue.push(node);
while (!myQueue.empty()) {
node = myQueue.front();
if (mySearchable->isGoalState(node)) {
return this->backTrace(node, mySearchable);
}
myQueue.pop();
if (node->getCost() == -1) {
node->setColor('b');
continue;
}
myVec = mySearchable->getAllPossibleStates(node);
typename vector<State<T> *>::iterator it = myVec.begin();
for (it; it < myVec.end(); it++) {
State<T> *adj = *it;
if (adj->getColor() == 'w') {
adj->setColor('g');
adj->setCamefrom(node);
myQueue.push(adj);
}
}
this->nodesEvaluated++;
node->setColor('b');
}
}
BFS<T> *clone() override {
return new BFS<T>();
}
};
#endif //EX4_BFS_H