Search algorithms for the 8-puzzle solution
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 
18  // use a queue to organize states, states that were reached first will be the first ones to be evaluated
20 
21  const clock_t start = clock();
23 
24  int currentDepth = 0;
25 
26  cout << "Depth\tVisited\t\tExpanded\n";
27  while (! expanded.isEmpty()) {
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) {
39  cout << currentGame->getDepth() << "\t\t" << visited.getSize() << "\t\t\t" << expanded.getSize() << endl;
40  }
41 
42  //expand children
45  }
46  throw invalid_argument("This game is unsolvable!");
47  }
48 };
49 
50 #endif // SEARCH_BREADTHFIRSTSOLVER_HPP
LinkedList< GameState * > solve(Game &game, GameState &g0)
Explores the game tree in search of the goal state.
virtual LinkedList< GameState * > solve(Game &g, GameState &gs)=0
Explores the game tree in search of the goal state.