|
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: FOUR_MOVES (the four sides of a cell), EIGHT_MOVES (the sides and the corners, the king) and KNIGHT_MOVES (the knight). More...
#include "ADS/Contract.h"#include "ADS/graph/Visit.h"#include <array>#include <cstddef>#include <type_traits>#include <utility>#include <vector>#include "ADS/graph/impl/Grid.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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: FOUR_MOVES (the four sides of a cell), EIGHT_MOVES (the sides and the corners, the king) and KNIGHT_MOVES (the knight).
The graph is never built: the cells are its nodes and a vector of moves its adjacency, so that each visit takes \( \Theta( R C ) \) time for k moves per cell treated as a constant, against the \( \Theta( R C k ) \) space of the lists of adjacency that it saves.
The functions take the grid as its size and a predicate of the free cells, and the moves as any range of differences, the knight's too; GridGraph [see GridGraph.h] is instead a graph of the library, the map stored as one vector of costs and the moves those of four or eight directions, on which the visits of Visit.h and the shortest paths of ShortestPaths.h run, and gridDistances() has an overload on it. There is none of floodFill(): the paint bucket reads and writes the colors of the matrix itself, and the region that it paints is the set of the cells reached by a visit of the grid whose free cells are those of the color of the start, which distance() of Visit.h gives on a GridGraph of that map.
The contracts [see Contract.h]: the grid has rows, cols >= 0, the matrices are rectangular, each source or start is a cell of the grid (and free, for the visits); at the level of the audits gridDistances() ensures that its distances are those of a breadth first visit (0 on the sources, and each reached cell but a source one move farther than a neighbor). The functions are defined in Grid.tpp.