-
Notifications
You must be signed in to change notification settings - Fork 2
/
Protecting the Hive.cpp
77 lines (75 loc) · 1.75 KB
/
Protecting the Hive.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
73
74
75
76
77
#include <iostream>
using namespace std;
pair<int,int> root[111][111];
int cnt[111][111];
bool activated[111][111];
pair<int,int> findroot(int x,int y) {
auto p = root[x][y];
if(p.first == x && p.second == y)
return p;
p = findroot(p.first, p.second);
root[x][y] = p;
return p;
}
void join(int x1, int y1, int x2, int y2) {
if(!activated[x2][y2])
return;
auto p = findroot(x2, y2);
if(p.first == x1 && p.second == y1) {
cnt[x1][y1] -= 2;
} else {
cnt[x1][y1] += cnt[p.first][p.second] - 2;
root[p.first][p.second] = {x1,y1};
}
}
void activate(int x,int y) {
cnt[x][y] = 6;
join(x, y, x - 1, y);
join(x, y, x + 1, y);
join(x, y, x, y -1);
join(x, y, x, y + 1);
if(x & 1) {
join(x, y, x - 1, y - 1);
join(x, y, x + 1, y - 1);
} else {
join(x, y, x - 1, y + 1);
join(x, y, x + 1, y + 1);
}
activated[x][y] =true;
}
int main() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j =1; j <= m; j++)
root[i][j] = {i,j};
for(int i = 1; i <= n; i++) {
int k;
if(i&1)
for(int j = 1; j <= m; j++) {
cin >> k;
if(k)
activate(i, j);
}
else
for(int j = 1; j < m; j++) {
cin >> k;
if(k)
activate(i, j);
}
}
int q;
cin >> q;
for(int i = 0; i < q; i++) {
string s;
int x, y;
cin >> s >> x >> y;
if(s[0] == 'a') {
activate(x, y);
} else {
auto p = findroot(x, y);
cout << cnt[p.first][p.second] << endl;
}
}
return 0;
}