-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.h
56 lines (51 loc) · 1.36 KB
/
DFS.h
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
//
// Created by shachar Meir on 04/02/2020.
//
#ifndef TEST__DFS_H_
#define TEST__DFS_H_
#include <queue>
#include <stack>
#include <set>
#include "Searcher.h"
template <typename T>
class DFS : public Searcher<T>{
public:
std::list<State<T> *> search(Searchable<T> *searchable) override {
searchable->reset();
std::stack<State<T>*> stack;
State<T>* start = searchable->getInitialState();
stack.push(start);
if (searchable->isGoal(start)){
auto path = this->backTracePath(start);
searchable->reset();
return path;
}
while (!stack.empty()) {
State<T>* next = stack.top();
stack.pop();
if (searchable->isGoal(next)){
auto path = this->backTracePath(next);
searchable->reset();
return path;
}
std::list<State<T> *> neighbors = searchable->getNeighborStates(next);
//next->was_visited = true;
for (State<T> *neighbor: neighbors) {
if (neighbor->GetCameFrom() == nullptr){
neighbor->SetCameFrom(next);
stack.push(neighbor);
if (searchable->isGoal(neighbor)){
auto path = this->backTracePath(neighbor);
searchable->reset();
return path;
}
}
}
}
return std::list<State<T> *>();
}
int getNumberOfNodesEvaluated() override {
return 0;
}
};
#endif //TEST__DFS_H_