ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Grid.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Grid.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
84/*--------------------------------------------------------------------------*/
85/*----------------------------- DEFINITIONS --------------------------------*/
86/*--------------------------------------------------------------------------*/
87
88#ifndef __ADS_Grid
89 #define __ADS_Grid
90/* self-identification: #endif constant */
91
92/*--------------------------------------------------------------------------*/
93/*------------------------------ INCLUDES ----------------------------------*/
94/*--------------------------------------------------------------------------*/
95
96#include "ADS/Contract.h"
97#include "ADS/graph/Visit.h"
98
99#include <array>
100#include <cstddef>
101#include <type_traits>
102#include <utility>
103#include <vector>
104
105/*--------------------------------------------------------------------------*/
106/*--------------------------- NAMESPACE ------------------------------------*/
107/*--------------------------------------------------------------------------*/
108
109namespace ADS
110{
111
114using GridCell = std::pair< int , int >;
115
117inline constexpr std::array< GridCell , 4 > FOUR_MOVES = {
118 GridCell( -1 , 0 ) , GridCell( 0 , -1 ) , GridCell( 1 , 0 ) ,
119 GridCell( 0 , 1 )
120 };
121
123inline constexpr std::array< GridCell , 8 > EIGHT_MOVES = {
124 GridCell( -1 , -1 ) , GridCell( -1 , 0 ) , GridCell( -1 , 1 ) ,
125 GridCell( 0 , -1 ) ,
126 GridCell( 0 , 1 ) , GridCell( 1 , -1 ) , GridCell( 1 , 0 ) ,
127 GridCell( 1 , 1 )
128 };
129
131inline constexpr std::array< GridCell , 8 > KNIGHT_MOVES = {
132 GridCell( -2 , -1 ) , GridCell( -2 , 1 ) , GridCell( -1 , -2 ) ,
133 GridCell( -1 , 2 ) ,
134 GridCell( 1 , -2 ) , GridCell( 1 , 2 ) , GridCell( 2 , -1 ) ,
135 GridCell( 2 , 1 )
136 };
137
139using FourMoves = std::remove_cv_t< decltype( FOUR_MOVES ) >;
140
141/*--------------------------------------------------------------------------*/
142/*------------------------------- FUNCTIONS --------------------------------*/
143/*--------------------------------------------------------------------------*/
144
147inline bool insideGrid( int rows , int cols , GridCell x );
148
178template< class Free , class Moves = FourMoves >
179std::vector< std::vector< int > >
180gridDistances( int rows , int cols , const std::vector< GridCell > & sources ,
181 Free && free , const Moves & moves = FOUR_MOVES );
182
210template< class Free , class Moves = FourMoves >
211int gridDistance( int rows , int cols , GridCell s , GridCell t ,
212 Free && free ,
213 const Moves & moves = FOUR_MOVES );
214
240template< class T , class Moves = FourMoves >
241std::size_t floodFill( std::vector< std::vector< T > > & M , int r , int c ,
242 const std::type_identity_t< T > & color ,
243 const Moves & moves = FOUR_MOVES );
244
257inline int knightDistance( int n , GridCell s , GridCell t );
258
259/*--------------------------------------------------------------------------*/
260
261} // end( namespace ADS )
262
263/*--------------------------------------------------------------------------*/
264/*-------------------------- IMPLEMENTATION --------------------------------*/
265/*--------------------------------------------------------------------------*/
266
267#include "ADS/graph/impl/Grid.tpp"
268
269/*--------------------------------------------------------------------------*/
270
271#endif /* Grid.h included */
272
273/*--------------------------------------------------------------------------*/
274/*---------------------------- End File Grid.h -----------------------------*/
275/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The visits of a GRAPH of section 9.5 and their applications, each in time on lists or vectors of adj...
std::vector< std::vector< int > > 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 grid from the nearest source, INF if none reaches it,...
bool insideGrid(int rows, int cols, GridCell x)
whether is a cell of a grid
constexpr std::array< GridCell, 8 > EIGHT_MOVES
the moves to the sides and to the corners of a cell, those of the king
Definition Grid.h:123
std::pair< int, int > GridCell
a cell of a grid, or a move as the difference of two cells
Definition Grid.h:114
std::remove_cv_t< decltype(FOUR_MOVES) > FourMoves
the type of FOUR_MOVES, the default range of moves
Definition Grid.h:139
std::size_t 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 of and all the cells joined to it by moves through cells of its color get the color ; the ...
int gridDistance(int rows, int cols, GridCell s, GridCell t, Free &&free, const Moves &moves=FOUR_MOVES)
the distance in moves from the cell to the cell of the grid through the cells with ,...
int knightDistance(int n, GridCell s, GridCell t)
the fewest moves of a knight from to on an board, INF if it cannot get there
constexpr std::array< GridCell, 8 > KNIGHT_MOVES
the moves of the knight
Definition Grid.h:131
constexpr std::array< GridCell, 4 > FOUR_MOVES
the moves to the four sides of a cell: up, left, down, right
Definition Grid.h:117
Definition ADT.h:96