-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.h
30 lines (26 loc) · 776 Bytes
/
Grid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* Class that implements grid-related functions
*/
#include "Random.h"
using namespace std;
#ifndef GRID_H
#define GRID_H //header guards
class Grid //stores large 2D grids
{
private:
int *grid; //flatten it into 1D grid
int rows;
int cols;
static const int INF = int(1e9);
public:
Grid(int rows, int cols);
int getRows() const {return rows;}
int getCols() const {return cols;}
bool isValid(int x, int y) const {return (x>=0&&x<rows&&y>=0&&y<cols);} //check if the coordinates are in range
int at(int x, int y) const {return (isValid(x,y)?grid[getCols()*x+y]:-INF);} //get the number at position (x,y)
void update(int x, int y, int v){grid[getCols()*x+y]=v;}
void add(int x, int y, int v){grid[getCols()*x+y]+=v;}
void resetGrid();
~Grid();
};
#endif