|
ADS
Algorithms and Data Structures
|
The FIFTEEN PUZZLE of section 19.5 (the GIOCO DEL QUINDICI of the book, Samuel Loyd, 1878). More...
Files | |
| file | ADS/intractable/FifteenPuzzle.h |
| The FIFTEEN PUZZLE of section 19.5 (the GIOCO DEL QUINDICI of the book, Samuel Loyd, 1878): \( n^2 - 1 \) tiles numbered 1 to \( n^2 - 1 \) on an \( n \times n \) grid with a hole, a move slides a tile next to the hole into it, and the goal has the tiles in increasing order and the hole last. | |
Typedefs | |
| using | ADS::Board = std::vector< int > |
| a configuration: the tile of each square by rows, 0 the hole | |
Functions | |
| Board | ADS::puzzleGoal (int n) |
| the goal on \( n \times n \) squares, the tiles \( 1 , \ldots , n^2 - 1 \) by rows and the hole last | |
| int | ADS::manhattan (const Board &B, int n) |
| the lower bound of the text: the sum over the tiles of the rows and columns between the tile and its place, \( \sum_{B[ p ] \ne 0} | \lfloor p / n \rfloor - \lfloor q / n \rfloor |
+ | p \bmod n - q \bmod n | \) with \( q = B[ p ] - 1 \) | |
| std::vector< Board > | ADS::puzzleMoves (const Board &B, int n) |
| the configurations one move away, the hole swapped with the tile above, below, on the left and on the right of it, in this order | |
| Board | ADS::puzzleSpiralGoal (int n) |
| the spiral goal on \( n \times n \) squares: the tiles clockwise from the top left corner, the hole where the spiral ends | |
| std::optional< std::vector< Board > > | ADS::puzzleBFS (const Board &B, const Board &goal, int n, long *visited=nullptr) |
| the configurations from \( B \) to goal of a shortest solution by the visit in breadth, or nothing if goal cannot be reached; the configurations examined go to *visited, if given | |
| std::optional< std::vector< Board > > | ADS::puzzleDFS (const Board &B, const Board &goal, int n, int nmax) |
| the configurations from \( B \) to goal of a solution of at most \( \mathit{nmax} \) moves by the backtracking in depth, or nothing if there is none | |
| bool | ADS::solvable (const Board &B, int n) |
| whether the goal can be reached from \( B \) | |
| template<class Map = std::map< Board , int >> | |
| std::optional< std::vector< Board > > | ADS::solvePuzzle (const Board &B, int n, long *visited=nullptr) |
| the configurations from \( B \) to the goal of a shortest solution, or nothing; the number of nodes extracted goes to *visited, if given; the least level of each configuration is kept in a Map, std::map by default, or another map of std or Boost, or a hash table of ADS [see FlatHashTable.h], with the same nodes visited | |
The FIFTEEN PUZZLE of section 19.5 (the GIOCO DEL QUINDICI of the book, Samuel Loyd, 1878).
The definitions, the invariants and the costs of the area are in Theory of intractable problems.
| using ADS::Board = std::vector< int > |
a configuration: the tile of each square by rows, 0 the hole
|
inline |
the lower bound of the text: the sum over the tiles of the rows and columns between the tile and its place, \( \sum_{B[ p ] \ne 0} | \lfloor p / n \rfloor - \lfloor q / n \rfloor | + | p \bmod n - q \bmod n | \) with \( q = B[ p ] - 1 \)
|
inline |
the configurations from \( B \) to goal of a shortest solution by the visit in breadth, or nothing if goal cannot be reached; the configurations examined go to *visited, if given
In pseudocode (the visit in breadth of section 9.5 on the graph of the configurations, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{puzzleBFS}( B , \mathit{goal} ) \cr 2 & \quad p[ B ] \gets B;\ Q \gets ( B ) \cr 3 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 4 & \qquad u \gets \mathrm{dequeue}( Q ) \cr 5 & \qquad \textbf{if}\ u = \mathit{goal}\ \textbf{then return} \ \text{the path from } B \text{ to } u \text{ by } p \cr 6 & \qquad \textbf{foreach}\ v \in \mathrm{puzzleMoves}( u ) \text{ with no } p[ v ]\ \textbf{do}\ p[ v ] \gets u; \ \mathrm{enqueue}( Q , v ) \cr 7 & \quad \textbf{return nil} \end{array}} \]
|
inline |
the configurations from \( B \) to goal of a solution of at most \( \mathit{nmax} \) moves by the backtracking in depth, or nothing if there is none
In pseudocode (the backtracking of chapter 16, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( S )\quad \triangleright \ \text{started with } S = ( B ) \cr 2 & \quad \textbf{if}\ \text{the last of } S \text{ is } \mathit{goal} \ \textbf{then return true} \cr 3 & \quad \textbf{if}\ |S| > \mathit{nmax} \ \textbf{then return false}\quad \triangleright\ \mathit{nmax} \text{ moves made} \cr 4 & \quad \textbf{foreach}\ v \in \mathrm{puzzleMoves}( \text{the last of } S ) \text{ with } v \notin S\ \textbf{do} \cr 5 & \qquad \textbf{if}\ \mathrm{extend}( S \cdot ( v ) ) \ \textbf{then return true} \cr 6 & \quad \textbf{return false} \end{array}} \]
|
inline |
the goal on \( n \times n \) squares, the tiles \( 1 , \ldots , n^2 - 1 \) by rows and the hole last
the configurations one move away, the hole swapped with the tile above, below, on the left and on the right of it, in this order
|
inline |
the spiral goal on \( n \times n \) squares: the tiles clockwise from the top left corner, the hole where the spiral ends
|
inline |
whether the goal can be reached from \( B \)
In pseudocode (section 19.5, after Johnson and Story), a move changes the parity of the inversions when \( n \) is even and the hole changes row, and never otherwise:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{solvable}( B , n )\quad \triangleright \ \text{a move keeps the parity of the quantity below} \cr 2 & \quad \mathit{inv} \gets \text{the pairs } p < q \text{ of tiles, the hole left out, with } B[ q ] < B[ p ] \cr 3 & \quad \textbf{return}\ \mathit{inv} \text{ even if } n \text{ is odd, else } \mathit{inv} + n - 1 - r \text{ even, } r \text{ the row of the hole} \end{array}} \]
| std::optional< std::vector< Board > > ADS::solvePuzzle | ( | const Board & | B, |
| int | n, | ||
| long * | visited = nullptr ) |
the configurations from \( B \) to the goal of a shortest solution, or nothing; the number of nodes extracted goes to *visited, if given; the least level of each configuration is kept in a Map, std::map by default, or another map of std or Boost, or a hash table of ADS [see FlatHashTable.h], with the same nodes visited
In pseudocode (section 19.5), \( g( x ) \) the moves from \( B \) to the node \( x \) and \( \mathit{best} \) the least level met of each configuration, an entry of the queue with a larger one being stale:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{solvePuzzle}( B , n )\quad \triangleright \ \text{A* with the Manhattan distance} \cr 2 & \quad \textbf{if}\ \textbf{not}\ \mathrm{solvable}( B , n ) \ \textbf{then return nil} \cr 3 & \quad \mathit{best}[ B ] \gets 0;\ Q \gets \text{a min-heap holding } B \text{ with priority } \mathrm{manhattan}( B ) \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad x \gets \mathrm{deleteMin}( Q );\ \textbf{if}\ \mathit{best}[ x ] < g( x )\ \textbf{then continue}\quad \triangleright \ \text{an older copy of } x \cr 6 & \qquad \textbf{if}\ x \text{ is the goal}\ \textbf{then return} \ \text{the moves from } B \text{ to } x \cr 7 & \qquad \textbf{foreach}\ y \in \mathrm{puzzleMoves}( x ) \text{ with } g( x ) + 1 < \mathit{best}[ y ]\ \textbf{do}\quad \triangleright\ \mathit{best} \text{ is } \infty \text{ where unset} \cr 8 & \qquad\quad \mathit{best}[ y ] \gets g( x ) + 1;\ \mathrm{insert}( Q , y , \mathit{best}[ y ] + \mathrm{manhattan}( y ) ) \cr 9 & \quad \textbf{return nil} \end{array}} \]