|
ADS
Algorithms and Data Structures
|
The visits of a GRID, the implicit graph whose nodes are the cells \( ( r , c ) \) of a \( \mathit{rows} \times \mathit{cols} \) matrix and whose edges are the moves from a cell to the cells at the given differences \( ( \mathit{dr} , \mathit{dc} ) \) that are inside the matrix and free. More...
Files | |
| file | ADS/graph/Grid.h |
| The visits of a GRID, the implicit graph whose nodes are the cells \( ( r , c ) \) of a \( \mathit{rows} \times \mathit{cols} \) matrix and whose edges are the moves from a cell to the cells at the given differences \( ( \mathit{dr} , \mathit{dc} ) \) that are inside the matrix and free: FOUR_MOVES (the four sides of a cell), EIGHT_MOVES (the sides and the corners, the king) and KNIGHT_MOVES (the knight). | |
Typedefs | |
| using | ADS::GridCell = std::pair< int , int > |
| a cell \( ( r , c ) \) of a grid, or a move as the difference of two cells | |
| using | ADS::FourMoves = std::remove_cv_t< decltype( FOUR_MOVES ) > |
| the type of FOUR_MOVES, the default range of moves | |
Functions | |
| bool | ADS::insideGrid (int rows, int cols, GridCell x) |
| whether \( ( r , c ) \) is a cell of a \( \mathit{rows} \times \mathit{cols} \) grid | |
| template<class Free, class Moves = FourMoves> | |
| std::vector< std::vector< int > > | ADS::gridDistances (int rows, int cols, const std::vector< GridCell > &sources, Free &&free, const Moves &moves=FOUR_MOVES) |
| the distance in moves of each cell of the \( \mathit{rows} \times \mathit{cols} \) grid from the nearest source, INF if none reaches it, entering only the cells \( x \) with \( \mathrm{free}( x ) \); \( D[ r ][ c ] \) for the cell \( ( r , c ) \) | |
| template<class Free, class Moves = FourMoves> | |
| int | ADS::gridDistance (int rows, int cols, GridCell s, GridCell t, Free &&free, const Moves &moves=FOUR_MOVES) |
| the distance in moves from the cell \( s \) to the cell \( t \) of the \( \mathit{rows} \times \mathit{cols} \) grid through the cells \( x \) with \( \mathrm{free}( x ) \), INF if \( t \) cannot be reached; the visit stops when \( t \) leaves the queue | |
| template<class T, class Moves = FourMoves> | |
| std::size_t | ADS::floodFill (std::vector< std::vector< T > > &M, int r, int c, const std::type_identity_t< T > &color, const Moves &moves=FOUR_MOVES) |
| the cell \( ( r , c ) \) of \( M \) and all the cells joined to it by moves through cells of its color get the color \( \mathit{color} \); the number of cells painted, 0 if \( \mathit{color} \) is already the color of \( ( r , c ) \) | |
| int | ADS::knightDistance (int n, GridCell s, GridCell t) |
| the fewest moves of a knight from \( s \) to \( t \) on an \( n \times n \) board, INF if it cannot get there | |
Variables | |
| constexpr std::array< GridCell, 4 > | ADS::FOUR_MOVES |
| the moves to the four sides of a cell: up, left, down, right | |
| constexpr std::array< GridCell, 8 > | ADS::EIGHT_MOVES |
| the moves to the sides and to the corners of a cell, those of the king | |
| constexpr std::array< GridCell, 8 > | ADS::KNIGHT_MOVES |
| the moves of the knight | |
The visits of a GRID, the implicit graph whose nodes are the cells \( ( r , c ) \) of a \( \mathit{rows} \times \mathit{cols} \) matrix and whose edges are the moves from a cell to the cells at the given differences \( ( \mathit{dr} , \mathit{dc} ) \) that are inside the matrix and free.
The definitions, the invariants and the costs of the area are in Theory of graphs.
| using ADS::FourMoves = std::remove_cv_t< decltype( FOUR_MOVES ) > |
the type of FOUR_MOVES, the default range of moves
| using ADS::GridCell = std::pair< int , int > |
a cell \( ( r , c ) \) of a grid, or a move as the difference of two cells
| std::size_t ADS::floodFill | ( | std::vector< std::vector< T > > & | M, |
| int | r, | ||
| int | c, | ||
| const std::type_identity_t< T > & | color, | ||
| const Moves & | moves = FOUR_MOVES ) |
the cell \( ( r , c ) \) of \( M \) and all the cells joined to it by moves through cells of its color get the color \( \mathit{color} \); the number of cells painted, 0 if \( \mathit{color} \) is already the color of \( ( r , c ) \)
In pseudocode, the color itself marking the cells reached:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{floodFill}( M , r , c , \mathit{color} ) \cr 2 & \quad a \gets M[ r ][ c ] \cr 3 & \quad \textbf{if}\ a = \mathit{color}\ \textbf{then return}\ 0 \cr 4 & \quad M[ r ][ c ] \gets \mathit{color};\ Q \gets ( ( r , c ) );\ k \gets 1 \cr 5 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 6 & \qquad x \gets \mathrm{dequeue}( Q ) \cr 7 & \qquad \textbf{foreach}\ \delta \in M\ \textbf{do} \cr 8 & \qquad\quad \textbf{if}\ x + \delta \text{ is in } M\ \textbf{and}\ M[ x + \delta ] = a\ \textbf{then} \cr 9 & \qquad\qquad M[ x + \delta ] \gets \mathit{color};\ \mathrm{enqueue}( Q , x + \delta );\ k \gets k + 1 \cr 10 & \quad \textbf{return}\ k \end{array}} \]
| int ADS::gridDistance | ( | int | rows, |
| int | cols, | ||
| GridCell | s, | ||
| GridCell | t, | ||
| Free && | free, | ||
| const Moves & | moves = FOUR_MOVES ) |
the distance in moves from the cell \( s \) to the cell \( t \) of the \( \mathit{rows} \times \mathit{cols} \) grid through the cells \( x \) with \( \mathrm{free}( x ) \), INF if \( t \) cannot be reached; the visit stops when \( t \) leaves the queue
In pseudocode, the visit of gridDistances() from s alone, stopped when t leaves the queue:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gridDistance}( R , C , s , t , \mathit{free} , M ) \cr 2 & \quad \textbf{foreach}\ \text{cell } x\ \textbf{do}\ D[ x ] \gets \infty \cr 3 & \quad D[ s ] \gets 0;\ Q \gets ( s ) \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad x \gets \mathrm{dequeue}( Q ) \cr 6 & \qquad \textbf{if}\ x = t\ \textbf{then return}\ D[ t ] \cr 7 & \qquad \textbf{foreach}\ \delta \in M\ \textbf{with}\ x + \delta \text{ in the grid, free and } D[ x + \delta ] = \infty\ \textbf{do} \cr 8 & \qquad\quad D[ x + \delta ] \gets D[ x ] + 1;\ \mathrm{enqueue}( Q , x + \delta ) \cr 9 & \quad \textbf{return}\ \infty \end{array}} \]
| std::vector< std::vector< int > > ADS::gridDistances | ( | int | rows, |
| int | cols, | ||
| const std::vector< GridCell > & | sources, | ||
| Free && | free, | ||
| const Moves & | moves = FOUR_MOVES ) |
the distance in moves of each cell of the \( \mathit{rows} \times \mathit{cols} \) grid from the nearest source, INF if none reaches it, entering only the cells \( x \) with \( \mathrm{free}( x ) \); \( D[ r ][ c ] \) for the cell \( ( r , c ) \)
In pseudocode (the visit in breadth of section 9.5, from all the sources at once):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gridDistances}( R , C , S , \mathit{free} , M )\quad \triangleright\ M \text{ the moves} \cr 2 & \quad D[ x ] \gets \infty \text{ for every cell } x;\ Q \gets () \cr 3 & \quad \textbf{foreach}\ s \in S\ \textbf{do}\ D[ s ] \gets 0; \ \mathrm{enqueue}( Q , s )\quad \triangleright \ \text{all the sources at distance } 0 \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad x \gets \mathrm{dequeue}( Q ) \cr 6 & \qquad \textbf{foreach}\ \text{cell } y = x + \delta \text{, } \delta \in M \text{, inside the grid, free and with } D[ y ] = \infty \ \textbf{do} \cr 7 & \qquad\quad D[ y ] \gets D[ x ] + 1;\ \mathrm{enqueue}( Q , y ) \cr 8 & \quad \textbf{return}\ D \end{array}} \]
|
inline |
whether \( ( r , c ) \) is a cell of a \( \mathit{rows} \times \mathit{cols} \) grid
the fewest moves of a knight from \( s \) to \( t \) on an \( n \times n \) board, INF if it cannot get there
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{knightDistance}( n , s , t ) \cr 2 & \quad \textbf{return}\ \mathrm{gridDistance}( n , n , s , t , \text{every cell free} , \text{the moves of the knight} ) \end{array}} \]
|
inlineconstexpr |
|
inlineconstexpr |