-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
47 lines (42 loc) · 1.22 KB
/
BFS.cpp
File metadata and controls
47 lines (42 loc) · 1.22 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
#include <queue>
#include "BFS.h"
/**
* constructor for BFS.
*/
BFS::BFS() {}
/**
* finding the path from one node to another using the BFS algorithm.
* @param environment Environment*
* @param start Node*
* @param end Node*
*/
void BFS::findPath(Environment* environment, Node* start, Node* end) {
queue<Node*> path;
queue<Node*> toClearAllVisited;
Node* current = start;
path.push(current);
vector<Node*> neighbours;
start->setDaddy(NULL);
current->setVisited(true);
while(*current != end) {
//current->setVisited(true);
neighbours = (*environment).getNeighbours(current);
while (neighbours.size() != 0) {
neighbours.front()->setDaddy(current);
neighbours.front()->setVisited(true);
if (*neighbours.front() == end) {
return;
}
path.push(neighbours.front());
neighbours.erase(neighbours.begin());
}
toClearAllVisited.push(path.front());
path.pop();
current = path.front();
}
//Clear all the nodes' member - visited.
while (toClearAllVisited.size() > 0) {
toClearAllVisited.front()->setVisited(false);
toClearAllVisited.pop();
}
}