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