-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_functions.cpp
72 lines (64 loc) · 1.92 KB
/
grid_functions.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const int R = 1000, C = 1000;
int n, m; //init me
bool valid(int r, int c) { return r >= 0 && r <= n - 1 && c >= 0 && c <= m - 1; }
v<pair<int, int>> neighbours(int r, int c, bool sideOnly = 1) {
v<pair<int, int>> res;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (sideOnly && ((i != 0) ^ (j != 0)) && valid(r + i, c + j))
res.pb({r + i, c + j});
else if (!sideOnly && ((i != 0) || (j != 0)) && valid(r + i, c + j))
res.pb({r + i, c + j});
return res;
}
int getHash(int r, int c) { return r * m + c; }
vector<int> g[R * C];
void makeGraph(vector<vector<int>> grid) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (grid[i][j]) {
g[getHash(i, j)].clear();
for (auto k : neighbours(i, j))
if (grid[i][j] == grid[k.fs][k.sc])
g[getHash(i, j)].pb(getHash(k.fs, k.sc));
}
}
pair<int, int> getCoordinate(int vertexNumber) { return {vertexNumber / m, vertexNumber % m}; }
vector<vector<int>> rotate(vector<vector<int>> grid) {
int nn = grid.size();
vector<vector<int>> res(nn, vector<int>(nn));
for (int i = 0; i < nn / 2 + nn % 2; i++) {
int cr = i, cc = i, pr = i, pc = nn - i - 1;
for (int z = 0; z < 4; z++) {
for (int j = 0; j < nn - i * 2 - 1; j++) {
res[cr][cc] = grid[pr][pc];
if (z == 0) {
cc++;
pr++;
} else if (z == 1) {
cr++;
pc--;
} else if (z == 2) {
cc--;
pr--;
} else {
cr--;
pc++;
}
}
res[cr][cc] = grid[pr][pc];
}
}
return res;
}
void dropdown(vector<vector<int>> grid) {
for (int j = 0; j < m; j++) {
int p = n - 1;
for (int i = n - 1; i >= 0; i--)
if (grid[i][j]) {
int v = grid[i][j];
grid[i][j] = 0;
grid[p--][j] = v;
}
}
}