Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int total_cows = 0;
- int inserted = 1;
- bool occupied[6040][6060];
- vector<pair<int, int>> adjacency = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
- vector<pair<int, int>> neighbors(int row, int column) {
- vector<pair<int, int>> adjacent;
- for (auto [row_diff, column_diff]: adjacency) {
- int next_row = row + row_diff, next_column = column + column_diff;
- if (occupied[next_row][next_column]) {
- adjacent.emplace_back(next_row, next_column);
- }
- }
- return adjacent;
- }
- bool check(int row, int column) {
- vector<pair<int, int>> adjacent = neighbors(row, column);
- if (adjacent.size() == 3) {
- return true;
- }
- return false;
- }
- void search(int start_row, int start_column) {
- stack<pair<int, int>> dfs;
- dfs.emplace(start_row, start_column);
- set<pair<int, int>> visited;
- while (!dfs.empty()) {
- auto [row, column] = dfs.top();
- dfs.pop();
- if (visited.count({row, column})) {
- continue;
- }
- visited.emplace(row, column);
- int adjacency_count = neighbors(row, column).size();
- vector<pair<int, int>> adjacent;
- for (auto [row_diff, column_diff]: adjacency) {
- int next_row = row + row_diff, next_column = column + column_diff;
- if (adjacency_count == 3 && !occupied[next_row][next_column]) {
- occupied[next_row][next_column] = true;
- total_cows++;
- }
- if (occupied[next_row][next_column]) {
- adjacent.emplace_back(next_row, next_column);
- }
- }
- for (auto [next_row, next_column]: adjacent) {
- if (check(next_row, next_column)) {
- dfs.emplace(next_row, next_column);
- }
- auto adj = neighbors(next_row, next_column);
- for(auto[r, c] : adj) {
- if(check(r, c)) {
- dfs.emplace(r, c);
- }
- }
- }
- }
- }
- void setIO(string s) {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- freopen((s + ".in").c_str(), "r", stdin);
- freopen((s + ".out").c_str(), "w", stdout);
- }
- int main() {
- // setIO("test");
- int amount;
- cin >> amount;
- for (; inserted <= amount; inserted++) {
- int row, column;
- cin >> column >> row;
- row += 1000; column += 1000;
- if (occupied[row][column]) {
- cout << total_cows - inserted << "\n";
- continue;
- }
- occupied[row][column] = true;
- total_cows++;
- if(check(row, column)) search(row, column);
- auto adj = neighbors(row, column);
- for(auto[r, c] : adj) {
- if(check(r, c)) search(r, c);
- }
- cout << total_cows - inserted << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment