-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.h
62 lines (56 loc) · 1.81 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
57
58
59
60
61
62
/*****************************
* Project part B of:
* Ori Kopel 205533151
* Shlomo Rabinovich 308432517
* Januar 2019
******************************/
#ifndef NEWPROJ_DFS_H
#define NEWPROJ_DFS_H
#include "Searcher.h"
#include <list>
#include "Point.h"
using std::list;
class DFS : public Searcher {
public:
vector<Point *> *search(Searchable *searchable) override {
// create database
auto *blacks = new list<Point *>;
auto *grays = new list<Point *>;
Point *begin = searchable->getInition();
Point *goal = this->visit(begin, blacks, grays, searchable);
if (goal == searchable->getGoal()) {
cout << "DFS:\n num of points which eq" << this->gettotalCost() << endl;
return this->backTrace(searchable->getInition(), goal);
}
// delete blacks;
// delete grays;
return nullptr;
}
Point *visit(Point *state, list<Point *> *blacks, list<Point *> *grays, Searchable *searchable) {
this->totalCost++;
// stop condition
if (searchable->getGoal()->equal(state)) { return state; }
grays->push_back(state);
auto *adjs = searchable->getAllPossibleStates(searchable, state);
for (auto a : *adjs) {
if (a == nullptr || a->getCost() == -1) {
continue;
}
bool isWhite = true;
for (auto v:*grays) {
if (v->equal(a)) {
isWhite = false;
}
}
if (isWhite) {
a->setCameFrom(state);
Point *tmp = this->visit(a, blacks, grays, searchable);
if (tmp == searchable->getGoal()) {
return tmp;
}
}
}
blacks->push_back(state);
}
};
#endif //NEWPROJ_DFS_H