ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
NQueens.h

The \( n \) queens (exercise 16.5). More...

Files

file  ADS/backtrack/NQueens.h
 The \( n \) queens (exercise 16.5): \( n \) queens on an \( n \times n \) board, no two on a row, a column or a diagonal.

Functions

template<class F>
long ADS::nQueens (int n, F &&f)
 the placements of \( n \le 32 \) queens, f( Q ) on each, \( Q[ r ] \) the column of the queen of the row \( r \), and true from f to stop; the number found
template<class F>
long ADS::nQueensIterative (int n, F &&f)
 the placements of \( n \) queens by the iterative backtracking, with the stage \( k \) and the column \( V[ k ] \) of the queen of the row \( k \), f( V ) on each and true from f to stop; the number found
bool ADS::admissible (const std::vector< int > &Q)
 whether no two queens of \( Q \) attack each other, \( Q[ r ] \) the column of the queen of the row \( r \)
std::string ADS::board (const std::vector< int > &Q)
 the board of \( Q \), a line per row, with the letter Q for a queen and a dot for an empty square

Detailed description

The \( n \) queens (exercise 16.5).

The definitions, the invariants and the costs of the area are in Theory of backtracking.

Function documentation

◆ admissible()

bool ADS::admissible ( const std::vector< int > & Q)
inline

whether no two queens of \( Q \) attack each other, \( Q[ r ] \) the column of the queen of the row \( r \)

In pseudocode (exercise 16.5), in \( O( n^2 ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{admissible}( Q ) \cr 2 & \quad \textbf{return}\ \text{whether } Q[ i ] \ne Q[ j ] \text{ and } | Q[ j ] - Q[ i ] | \ne j - i \text{ for every } i < j \end{array}} \]

◆ board()

std::string ADS::board ( const std::vector< int > & Q)
inline

the board of \( Q \), a line per row, with the letter Q for a queen and a dot for an empty square

Precondition
each column in \( [ 0 , n ) \)

◆ nQueens()

template<class F>
long ADS::nQueens ( int n,
F && f )

the placements of \( n \le 32 \) queens, f( Q ) on each, \( Q[ r ] \) the column of the queen of the row \( r \), and true from f to stop; the number found

In pseudocode (exercise 16.5), the columns and the two diagonals attacked on the row \( r \) kept as sets \( C \), \( D_1 \) and \( D_2 \) (masks of bits in the code), which move by one column at each row:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{queens}( r , C , D_1 , D_2 )\quad \triangleright\ \text{the columns of the row } r \text{ that the queens above attack} \cr 2 & \quad \textbf{if}\ r = n\ \textbf{then return}\ f( Q ) \cr 3 & \quad \textbf{foreach}\ c \notin C \cup D_1 \cup D_2 \text{, from } 0 \text{ to } n - 1\ \textbf{do} \cr 4 & \qquad Q[ r ] \gets c\quad \triangleright \ \text{the diagonals move one column at the next row} \cr 5 & \qquad D'_1 \gets \{ x + 1 : x \in D_1 \cup \{ c \} \} \cap [ 0 , n );\ D'_2 \gets \{ x - 1 : x \in D_2 \cup \{ c \} \} \cap [ 0 , n ) \cr 6 & \qquad \textbf{if}\ \mathrm{queens}( r + 1 , C \cup \{ c \} , D'_1 , D'_2 )\ \textbf{then return true} \cr 7 & \quad \textbf{return false} \end{array}} \]

Precondition
\( 1 \le n \le 32 \)
Postcondition
each solution is admissible (audit)

◆ nQueensIterative()

template<class F>
long ADS::nQueensIterative ( int n,
F && f )

the placements of \( n \) queens by the iterative backtracking, with the stage \( k \) and the column \( V[ k ] \) of the queen of the row \( k \), f( V ) on each and true from f to stop; the number found

In pseudocode (exercise 16.5, without recursion): \( V[ k ] \) goes to the next column not attacked, \( k \) goes on when one is found and back when the columns of the row \( k \) are over.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nQueensIterative}( n )\quad \triangleright\ V[ k ] \text{ the column of row } k \text{, } -1 \text{ none yet} \cr 2 & \quad V \gets ( -1 , \ldots , -1 );\ k \gets 0 \cr 3 & \quad \textbf{while}\ k \ge 0\ \textbf{do} \cr 4 & \qquad V[ k ] \gets \text{the first column after } V[ k ] \text{ with } \mathrm{free}( k ) \text{, or } n \text{ if none} \cr 5 & \qquad \textbf{if}\ V[ k ] = n\ \textbf{then}\ V[ k ] \gets -1;\ k \gets k - 1\quad \triangleright\ \text{back to the row above} \cr 6 & \qquad \textbf{else if}\ k < n - 1\ \textbf{then}\ k \gets k + 1 \cr 7 & \qquad \textbf{else if}\ f( V )\ \textbf{then return}\quad \triangleright\ \text{a solution, and the visitor stops} \cr 8 & \textbf{function}\ \mathrm{free}( k )\quad \triangleright \ \text{no queen above shares a column or a diagonal} \cr 9 & \quad \textbf{return}\ V[ h ] \ne V[ k ] \text{ and } | V[ k ] - V[ h ] | \ne k - h \text{ for every } h < k \end{array}} \]

Precondition
\( n \ge 1 \)
Postcondition
each solution is admissible (audit)