#include <iostream> #include <vector> using namespace std; int n, m, k, q; int Rozwiazanie(vector<vector<int>> pola) { int wynik = 0; bool ruch = true; do { ruch = false; for (int i = 0; i < n; ++i) { for (int d = 0; d < m; ++d) { if (pola[i][d] == 1) { // rogi if (d == 0 && i == 0) { if (pola[i][d + 1] == 0 || pola[i + 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == 0 && i == n - 1) { if (pola[i][d + 1] == 0 || pola[i - 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == m - 1 && i == 0) { if (pola[i][d - 1] == 0 || pola[i + 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == m - 1 && i == n - 1) { if (pola[i][d - 1] == 0 || pola[i - 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } // ściany else if (i == 0) { if (pola[i + 1][d] == 0 || (pola[i][d + 1] == 0 && pola[i][d - 1] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == 0) { if (pola[i][d + 1] == 0 || (pola[i + 1][d] == 0 && pola[i - 1][d] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (i == n - 1) { if (pola[i - 1][d] == 0 || (pola[i][d + 1] == 0 && pola[i][d - 1] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == m - 1) { if (pola[i][d - 1] == 0 || (pola[i + 1][d] == 0 && pola[i - 1][d] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } // po prostu klocek else { if ((pola[i + 1][d] == 0 && pola[i - 1][d] == 0) || (pola[i][d + 1] == 0 && pola[i][d - 1] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } } } } if (wynik == k) { break; } } while (ruch); return wynik; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k >> q; vector<vector<int>> pola(n); vector<int> wyniki; for (int i = 0; i < n; i++) { pola[i] = vector<int>(m, 0); // Tworzy rząd o m elementach, wypełniony zerami } for (int i = 0; i < k; ++i) { int y, x; cin >> x >> y; pola[x - 1][y - 1] = 1; } wyniki.push_back(Rozwiazanie(pola)); for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; x--, y--; pola[x][y] = 1 - pola[x][y]; wyniki.push_back(Rozwiazanie(pola)); } for (int i : wyniki) { cout << i << "\n"; } return 0; }
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include <iostream> #include <vector> using namespace std; int n, m, k, q; int Rozwiazanie(vector<vector<int>> pola) { int wynik = 0; bool ruch = true; do { ruch = false; for (int i = 0; i < n; ++i) { for (int d = 0; d < m; ++d) { if (pola[i][d] == 1) { // rogi if (d == 0 && i == 0) { if (pola[i][d + 1] == 0 || pola[i + 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == 0 && i == n - 1) { if (pola[i][d + 1] == 0 || pola[i - 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == m - 1 && i == 0) { if (pola[i][d - 1] == 0 || pola[i + 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == m - 1 && i == n - 1) { if (pola[i][d - 1] == 0 || pola[i - 1][d] == 0) { pola[i][d] = 0; ruch = true; ++wynik; } } // ściany else if (i == 0) { if (pola[i + 1][d] == 0 || (pola[i][d + 1] == 0 && pola[i][d - 1] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == 0) { if (pola[i][d + 1] == 0 || (pola[i + 1][d] == 0 && pola[i - 1][d] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (i == n - 1) { if (pola[i - 1][d] == 0 || (pola[i][d + 1] == 0 && pola[i][d - 1] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } else if (d == m - 1) { if (pola[i][d - 1] == 0 || (pola[i + 1][d] == 0 && pola[i - 1][d] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } // po prostu klocek else { if ((pola[i + 1][d] == 0 && pola[i - 1][d] == 0) || (pola[i][d + 1] == 0 && pola[i][d - 1] == 0)) { pola[i][d] = 0; ruch = true; ++wynik; } } } } } if (wynik == k) { break; } } while (ruch); return wynik; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k >> q; vector<vector<int>> pola(n); vector<int> wyniki; for (int i = 0; i < n; i++) { pola[i] = vector<int>(m, 0); // Tworzy rząd o m elementach, wypełniony zerami } for (int i = 0; i < k; ++i) { int y, x; cin >> x >> y; pola[x - 1][y - 1] = 1; } wyniki.push_back(Rozwiazanie(pola)); for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; x--, y--; pola[x][y] = 1 - pola[x][y]; wyniki.push_back(Rozwiazanie(pola)); } for (int i : wyniki) { cout << i << "\n"; } return 0; } |