|
ADS
Algorithms and Data Structures
|
The square tiling (BOUNDED TILING, the DOMINO LIMITATO of the book) of chapter 18. More...
Files | |
| file | ADS/complexity/Domino.h |
| The square tiling (BOUNDED TILING, the DOMINO LIMITATO of the book) of chapter 18: \( n \times n \) squares to cover with copies of \( m \) oriented tiles, each split by its diagonals into four colored quarters, never rotated, the tile \( d = D[ 0 ] \) in the corner \( ( 0 , 0 ) \), and touching sides of the same color. | |
Classes | |
| struct | ADS::Tile |
| a tile: the colors of its four sides More... | |
Functions | |
| bool | ADS::legalTiling (const std::vector< Tile > &D, int n, const Tiling &T) |
| whether \( T \) is a legal covering of the \( n \times n \) square by \( D \), with \( d = D[ 0 ] \) in \( ( 0 , 0 ) \); false if \( n < 1 \), if \( T \) is not \( n \times n \) or if a tile of \( T \) is not one of \( D \) | |
| std::optional< Tiling > | ADS::squareTiling (const std::vector< Tile > &D, int n) |
| a legal covering of the \( n \times n \) square, by backtrack, or nothing (also if \( n < 1 \) or \( D \) is empty) | |
| bool | ADS::linearTiling (const std::vector< Tile > &D, long n) |
| whether a strip of \( n \) tiles from \( d = D[ 0 ] \) exists, \( O( m^2 ) \); false if \( n < 1 \) or \( D \) is empty, true for any \( n \) if a cycle of tiles that fit is reachable from \( d \) | |
The square tiling (BOUNDED TILING, the DOMINO LIMITATO of the book) of chapter 18.
The definitions, the invariants and the costs of the area are in Theory of complexity.
whether \( T \) is a legal covering of the \( n \times n \) square by \( D \), with \( d = D[ 0 ] \) in \( ( 0 , 0 ) \); false if \( n < 1 \), if \( T \) is not \( n \times n \) or if a tile of \( T \) is not one of \( D \)
In pseudocode (chapter 18), the certificate in \( O( n^2 ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{legalTiling}( D , n , T )\quad \triangleright\ T[ i ][ j ] \text{ the tile of the square in column } i \text{, row } j \cr 2 & \quad \textbf{if}\ n < 1 \text{, or } T \text{ is not an } n \times n \text{ table of tiles of } D \text{, or } T[ 0 ][ 0 ] \ne 0 \ \textbf{then return false} \cr 3 & \quad \textbf{return}\ \text{whether every two neighbors match:} \cr & \qquad D[ T[ i ][ j ] ].\mathit{right} = D[ T[ i + 1 ][ j ] ].\mathit{left} \text{ and } D[ T[ i ][ j ] ].\mathit{up} = D[ T[ i ][ j + 1 ] ].\mathit{down} \cr & \qquad \text{wherever the second square exists} \end{array}} \]
|
inline |
whether a strip of \( n \) tiles from \( d = D[ 0 ] \) exists, \( O( m^2 ) \); false if \( n < 1 \) or \( D \) is empty, true for any \( n \) if a cycle of tiles that fit is reachable from \( d \)
In pseudocode (section 18.5), a visit in depth of the graph of the tiles from \( d \), \( L[ u ] \) the edges of the longest path from \( u \) in the acyclic part:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{visit}( u )\quad \triangleright \ \text{the graph of the tiles, } u \to v \text{ if } v \text{ fits right of } u \cr 2 & \quad \mathit{state}[ u ] \gets \text{open} \cr 3 & \quad \textbf{foreach}\ v \text{ with } D[ v ].\mathit{left} = D[ u ].\mathit{right}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathit{state}[ v ] = \text{new}\ \textbf{then} \ \mathrm{visit}( v )\ \textbf{else if}\ \mathit{state}[ v ] = \text{open}\ \textbf{then}\ \mathit{cycle} \gets \textbf{true} \cr 5 & \qquad L[ u ] \gets \max\{ L[ u ] , L[ v ] + 1 \}\quad \triangleright\ \text{the longest row from } u \cr 6 & \quad \mathit{state}[ u ] \gets \text{done} \cr 7 & \textbf{function}\ \mathrm{linearTiling}( D , n )\quad \triangleright\ \text{a row of } n \text{ tiles from the tile } 0 \cr 8 & \quad \textbf{if}\ n < 1\ \textbf{or}\ m = 0 \ \textbf{then return false} \cr 9 & \quad \mathrm{visit}( 0 );\ \textbf{return}\ \mathit{cycle} \text{ or } n - 1 \le L[ 0 ]\quad \triangleright \ \text{a cycle gives rows of any length} \end{array}} \]
a legal covering of the \( n \times n \) square, by backtrack, or nothing (also if \( n < 1 \) or \( D \) is empty)
In pseudocode (chapter 18), the squares filled by rows from the bottom, \( s = i + j n \), a tile placed only if it fits its left and lower neighbors:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{place}( s )\quad \triangleright \ \text{the squares } s = i + j n \text{ by rows, from } s = 0 \cr 2 & \quad \textbf{if}\ s = n^2\ \textbf{then return true} \cr 3 & \quad ( i , j ) \gets ( s \bmod n , \lfloor s / n \rfloor ) \cr 4 & \quad \textbf{foreach}\ \text{tile } t \text{, only } t = 0 \text{ on the first square}\ \textbf{do} \cr 5 & \qquad \textbf{if}\ t \text{ matches the tile on its left, if any, and the one below, if } \text{any}\ \textbf{then} \cr 6 & \qquad\quad T[ i ][ j ] \gets t;\ \textbf{if}\ \mathrm{place}( s + 1 )\ \textbf{then return true} \cr 7 & \quad \textbf{return false} \end{array}} \]