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 78 79 80 81 82 83 84 85 86 87
| #include <bits/stdc++.h> using namespace std;
const int N = 19; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; int board[20][20];
bool vis[20][20]; vector<pair<int, int>> trashbin;
int lib = 0, sum[2]; void dfs(const int &x, const int &y, const int &col) { if (vis[x][y]) return ; vis[x][y] = 1; trashbin.emplace_back(x, y); for (int i = 0, nxtx, nxty; i < 4; ++i) { nxtx = x + dx[i], nxty = y + dy[i]; if (nxtx < 1 || nxtx > N || nxty < 1 || nxty > N || vis[nxtx][nxty]) continue; if (board[nxtx][nxty] == -1) ++lib; if (board[nxtx][nxty] == col) dfs(nxtx, nxty, col); } return ; }
void calc(const int &x, const int &y, const int &col) { lib = 0; trashbin.clear(); dfs(x, y, col); if (!lib) { sum[col] += trashbin.size(); for (auto [x, y]: trashbin) board[x][y] = -1; } return ; }
void remove(const bool& who) { sum[0] = sum[1] = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { if (vis[i][j] || board[i][j] != (who ^ 1)) continue; calc(i, j, who ^ 1); } } memset(vis, 0, sizeof vis); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { if (vis[i][j] || board[i][j] != who) continue; calc(i, j, who); } } cout << sum[0] << ' ' << sum[1] << '\n'; return ; }
void work() { int m, x, y; cin >> m; bool who = 0; memset(board, -1, sizeof board); while (m--) { cin >> x >> y; board[x][y] = who; remove(who); who ^= 1; } return ; }
int main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; while (T--) work(); return 0; }
|