a_chn

Untitled

Jan 27th, 2025
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int total_cows = 0;
  5. int inserted = 1;
  6.  
  7. bool occupied[6040][6060];
  8. vector<pair<int, int>> adjacency = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  9.  
  10. vector<pair<int, int>> neighbors(int row, int column) {
  11.     vector<pair<int, int>> adjacent;
  12.  
  13.     for (auto [row_diff, column_diff]: adjacency) {
  14.         int next_row = row + row_diff, next_column = column + column_diff;
  15.  
  16.         if (occupied[next_row][next_column]) {
  17.             adjacent.emplace_back(next_row, next_column);
  18.         }
  19.     }
  20.  
  21.     return adjacent;
  22. }
  23.  
  24. bool check(int row, int column) {
  25.     vector<pair<int, int>> adjacent = neighbors(row, column);
  26.  
  27.     if (adjacent.size() == 3) {
  28.         return true;
  29.     }
  30.  
  31.     return false;
  32. }
  33.  
  34. void search(int start_row, int start_column) {
  35.     stack<pair<int, int>> dfs;
  36.     dfs.emplace(start_row, start_column);
  37.  
  38.     set<pair<int, int>> visited;
  39.  
  40.     while (!dfs.empty()) {
  41.         auto [row, column] = dfs.top();
  42.         dfs.pop();
  43.  
  44.         if (visited.count({row, column})) {
  45.             continue;
  46.         }
  47.  
  48.         visited.emplace(row, column);
  49.  
  50.         int adjacency_count = neighbors(row, column).size();
  51.         vector<pair<int, int>> adjacent;
  52.  
  53.         for (auto [row_diff, column_diff]: adjacency) {
  54.             int next_row = row + row_diff, next_column = column + column_diff;
  55.  
  56.             if (adjacency_count == 3 && !occupied[next_row][next_column]) {
  57.                 occupied[next_row][next_column] = true;
  58.                 total_cows++;
  59.             }
  60.  
  61.             if (occupied[next_row][next_column]) {
  62.                 adjacent.emplace_back(next_row, next_column);
  63.             }
  64.         }
  65.  
  66.         for (auto [next_row, next_column]: adjacent) {
  67.             if (check(next_row, next_column)) {
  68.                 dfs.emplace(next_row, next_column);
  69.             }
  70.             auto adj = neighbors(next_row, next_column);
  71.             for(auto[r, c] : adj) {
  72.                 if(check(r, c)) {
  73.                     dfs.emplace(r, c);
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. void setIO(string s) {
  81.     ios_base::sync_with_stdio(0);
  82.     cin.tie(0);
  83.     freopen((s + ".in").c_str(), "r", stdin);
  84.     freopen((s + ".out").c_str(), "w", stdout);
  85. }
  86.  
  87. int main() {
  88.     // setIO("test");
  89.     int amount;
  90.     cin >> amount;
  91.  
  92.     for (; inserted <= amount; inserted++) {
  93.         int row, column;
  94.         cin >> column >> row;
  95.  
  96.         row += 1000; column += 1000;
  97.  
  98.         if (occupied[row][column]) {
  99.             cout << total_cows - inserted << "\n";
  100.             continue;
  101.         }
  102.  
  103.         occupied[row][column] = true;
  104.         total_cows++;
  105.  
  106.         if(check(row, column)) search(row, column);
  107.         auto adj = neighbors(row, column);
  108.         for(auto[r, c] : adj) {
  109.             if(check(r, c)) search(r, c);
  110.         }
  111.         cout << total_cows - inserted << "\n";
  112.     }
  113.  
  114.     return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment