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

The Sudoku (reality check of section 16.3 and exercise 16.7), of size \( n^2 \times n^2 \) with the numbers \( 1 , \ldots , n^2 \), \( n = 3 \) the classic one. More...

Files

file  ADS/backtrack/Sudoku.h
 The Sudoku (reality check of section 16.3 and exercise 16.7), of size \( n^2 \times n^2 \) with the numbers \( 1 , \ldots , n^2 \), \( n = 3 \) the classic one: the squares are filled in order, each with the numbers that check() allows, no equal number on its row, its column and its block of \( n \times n \), and the search backtracks when a square has none.

Functions

bool ADS::check (const Grid &S, std::size_t x, std::size_t y, int c)
 whether \( c \) can be written in the square \( ( x , y ) \) of \( S \), i.e., it is not on its row, its column or its block
bool ADS::sudoku (Grid &S, std::size_t i=0)
 fills the empty squares of \( S \) from the square number \( i \) on, in the order of the rows; true if it can

Detailed description

The Sudoku (reality check of section 16.3 and exercise 16.7), of size \( n^2 \times n^2 \) with the numbers \( 1 , \ldots , n^2 \), \( n = 3 \) the classic one.

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

Function documentation

◆ check()

bool ADS::check ( const Grid & S,
std::size_t x,
std::size_t y,
int c )
inline

whether \( c \) can be written in the square \( ( x , y ) \) of \( S \), i.e., it is not on its row, its column or its block

In pseudocode (section 16.3), \( N = n^2 \) the side of the grid:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{check}( S , x , y , c )\quad \triangleright\ N = n^2 \text{, the blocks } n \times n \cr 2 & \quad \textbf{if}\ c \text{ is in the row } x \text{ or in the column } y\ \textbf{then return false} \cr 3 & \quad ( b_x , b_y ) \gets ( n \lfloor x / n \rfloor , n \lfloor y / n \rfloor )\quad \triangleright \ \text{the upper left square of the block} \cr 4 & \quad \textbf{return}\ \text{whether } c \text{ is in none of the squares } S[ b_x + i ][ b_y + j ] \text{, } 0 \le i , j < n \end{array}} \]

Precondition
\( ( x , y ) \) is a square and \( c \in [ 1 , n^2 ] \)

◆ sudoku()

bool ADS::sudoku ( Grid & S,
std::size_t i = 0 )
inline

fills the empty squares of \( S \) from the square number \( i \) on, in the order of the rows; true if it can

In pseudocode (section 16.3), \( N = n^2 \) the side of the grid and the square \( i \) that of row \( \lfloor i / N \rfloor \) and column \( i \bmod N \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sudoku}( S , i )\quad \triangleright \ \text{the squares in the order of the rows} \cr 2 & \quad \textbf{if}\ i = N^2\ \textbf{then return true} \cr 3 & \quad ( x , y ) \gets ( \lfloor i / N \rfloor , i \bmod N ) \cr 4 & \quad \textbf{if}\ S[ x ][ y ] \ne 0\ \textbf{then return} \ \mathrm{sudoku}( S , i + 1 )\quad \triangleright \ \text{a given number stays} \cr 5 & \quad \textbf{foreach}\ c \in [ 1 , N ] \text{ with } \mathrm{check}( S , x , y , c )\ \textbf{do} \cr 6 & \qquad S[ x ][ y ] \gets c;\ \textbf{if}\ \mathrm{sudoku}( S , i + 1 )\ \textbf{then return true} \cr 7 & \quad S[ x ][ y ] \gets 0;\ \textbf{return false}\quad \triangleright\ \text{the square is emptied on the way back} \end{array}} \]

Precondition
a grid of \( n^2 \times n^2 \) squares, the values given consistent, \( i \le n^4 \)
Postcondition
if true, a solution (audit)