-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomfcows.cpp
70 lines (52 loc) · 1.38 KB
/
comfcows.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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3;
using pi = pair<int, int>;
bool added[maxn][maxn];
int ans = 0;
void addpt(int x, int y);
void checknb(int x, int y);
void fixpt(int x, int y);
void addpt(int x, int y) {
// check if already added
if(added[x][y]) return;
added[x][y] = true;
ans++;
// when u add a cow two things can happen
fixpt(x, y);
checknb(x, y);
}
void checknb(int x, int y) {
pi nb[4] = {{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}};
for (int i = 0; i < 4; i++) {
if(added[nb[i].first][nb[i].second]) fixpt(nb[i].first, nb[i].second);
}
}
void fixpt(int x, int y) {
// fixing a point is equivalent to adding a cow where needed
pi nb[4] = {{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}};
int sum = 0;
pi need_to_fix;
for (int i = 0; i < 4; i++) {
int curr = added[nb[i].first][nb[i].second];
if (curr == 0) need_to_fix = nb[i];
sum += curr;
}
if(sum == 3) {
addpt(need_to_fix.first, need_to_fix.second);
}
}
int main() {
int n;
cin >> n;
memset(added, false, sizeof(added));
for(int i = 0; i < n; i++) {
pi pt;
cin >> pt.first >> pt.second;
pt.first += maxn / 2;
pt.second += maxn / 2;
addpt(pt.first, pt.second);
cout << ans - i - 1 << endl;
}
return 0;
}