Search algorithms for the 8-puzzle solution
include
BreadthFirstSolver.hpp
Go to the documentation of this file.
1
/**
2
* @author Douglas De Rizzo Meneghetti (douglasrizzom@gmail.com)
3
* @date 2017-6-23
4
* @brief 8-puzzle exploration based on a breadth-first search strategy
5
*/
6
7
#
ifndef
SEARCH_BREADTHFIRSTSOLVER_HPP
8
#
define
SEARCH_BREADTHFIRSTSOLVER_HPP
9
10
#
include
"Solver.hpp"
11
#
include
"DynamicQueue.hpp"
12
13
//! 8-puzzle exploration based on a breadth-first search strategy
14
class
BreadthFirstSolver
:
public
Solver
{
15
public
:
16
17
LinkedList
<
GameState
*>
solve
(
Game
&
game
,
GameState
&
g0
) {
18
// use a queue to organize states, states that were reached first will be the first ones to be evaluated
19
DynamicQueue
<
GameState
*>
expanded
;
20
21
const
clock_t
start
=
clock
();
22
expanded
.
enqueue
(&
g0
);
23
24
int
currentDepth
= 0;
25
26
cout
<<
"Depth\tVisited\t\tExpanded\n"
;
27
while
(!
expanded
.
isEmpty
()) {
28
GameState
*
currentGame
=
expanded
.
dequeue
();
29
30
// check for goal
31
if
(*
game
.
getGoal
() == *
currentGame
) {
32
solved
=
true
;
33
return
endSearch
(
currentGame
,
start
);
34
}
35
36
// if new depth, output a line to stdout
37
if
(
currentGame
->
getDepth
() !=
currentDepth
) {
38
currentDepth
=
currentGame
->
getDepth
();
39
cout
<<
currentGame
->
getDepth
() <<
"\t\t"
<<
visited
.
getSize
() <<
"\t\t\t"
<<
expanded
.
getSize
() <<
endl
;
40
}
41
42
//expand children
43
LinkedList
<
GameState
*>
children
=
visit
(
currentGame
);
44
while
(!
children
.
isEmpty
())
expanded
.
enqueue
(
children
.
remove
(0));
45
}
46
throw
invalid_argument
(
"This game is unsolvable!"
);
47
}
48
};
49
50
#
endif
// SEARCH_BREADTHFIRSTSOLVER_HPP
BreadthFirstSolver::solve
LinkedList< GameState * > solve(Game &game, GameState &g0)
Explores the game tree in search of the goal state.
Definition:
BreadthFirstSolver.hpp:17
Solver::solve
virtual LinkedList< GameState * > solve(Game &g, GameState &gs)=0
Explores the game tree in search of the goal state.
Generated by
1.8.13