-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.cpp
73 lines (61 loc) · 1.4 KB
/
BFS.cpp
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
#include "Sokoban.h"
bool Sokoban::BFS(Stage& current, list<Stage>& closedList)
{
time(&begin);
std::queue<Stage> queueStages;
int counter = 0;
closedList.push_back(current);
GetRobot(current);
do
{
if (!MoveRight(current).isDead)
{
queueStages.push(MoveRight(current));
}
if (!MoveUp(current).isDead)
{
queueStages.push(MoveUp(current));
}
if (!MoveLeft(current).isDead)
{
queueStages.push(MoveLeft(current));
}
if (!MoveDown(current).isDead)
{
queueStages.push(MoveDown(current));
}
//Display(current);
//cout << queueStages.size() << endl;
if (queueStages.size() != 0)
{
CopyStages(current, queueStages.front());
queueStages.pop();
while (CompareStages(current, closedList) && queueStages.size() != 0)
{
CopyStages(current, queueStages.front());
queueStages.pop();
PutSBack(current);
}
PutSBack(current);
closedList.push_back(current);
GetRobot(current);
}
else
{
cout << "BFS could not find a solution. Exiting now..." << endl;
return false;
}
} while (!CheckIfEnd(current));
time(&end);
cout << "BFS has found a solution. Outputting to BFS_Output.txt now..." << endl;
OutputList(closedList, "BFS");
while (!queueStages.empty())
{
queueStages.pop();
}
while (!closedList.empty())
{
closedList.pop_front();
}
return true;
}