#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<bool>> plansz ={};
queue<pair<int,int>> pionki = {};
int n, m, k, q ;
void print_v(vector<vector<bool>> const &plansz) {
for (auto w: plansz) {
for (auto p: w) {
if (p)
cout << "#";
else
cout << " ";
}
cout << endl;
}
}
bool test_w(int x, int y, vector<vector<bool>> const &plansz) {
return( (!plansz[x-1][y] && !plansz[x+1][y]) || (!plansz[x][y-1] && !plansz[x][y+1]) );
}
int licz_z(vector<vector<bool>> plansz, queue<pair<int,int>> pionki) {
int zdjetych = 0;
pair<int,int> p;
while (!pionki.empty()) {
p = pionki.front();
if (plansz[p.first][p.second]) {
if (test_w(p.first, p.second, plansz)) {
zdjetych++;
plansz[p.first][p.second] = false;
if (p.first>0) pionki.push({p.first-1,p.second});
if (p.first<n) pionki.push({p.first+1,p.second});
if (p.second>0) pionki.push({p.first,p.second-1});
if (p.second<m) pionki.push({p.first,p.second+1});
}
}
pionki.pop();
}
// print_v(plansz);
return (zdjetych);
}
int main()
{
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int x,y;
cin >> n >> m >> k >> q;
for (int i=0; i <= n+1; i++) {
plansz.push_back({});
for (int j=0; j<=m+1; j++) {
plansz[i].push_back(false);
}
}
for (int j=0; j<k; j++) {
cin >> x >> y;
plansz[x][y]=true;
pionki.push({x,y});
}
// print_v(plansz);
cout << licz_z(plansz, pionki) << endl;
for (int i=0; i<q; i++) {
cin >> x >> y;
plansz[x][y]=!plansz[x][y];
pionki.push({x,y});
// print_v(plansz);
cout << licz_z(plansz, pionki) << endl;
}
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 | #include <iostream> #include <vector> #include <queue> using namespace std; vector<vector<bool>> plansz ={}; queue<pair<int,int>> pionki = {}; int n, m, k, q ; void print_v(vector<vector<bool>> const &plansz) { for (auto w: plansz) { for (auto p: w) { if (p) cout << "#"; else cout << " "; } cout << endl; } } bool test_w(int x, int y, vector<vector<bool>> const &plansz) { return( (!plansz[x-1][y] && !plansz[x+1][y]) || (!plansz[x][y-1] && !plansz[x][y+1]) ); } int licz_z(vector<vector<bool>> plansz, queue<pair<int,int>> pionki) { int zdjetych = 0; pair<int,int> p; while (!pionki.empty()) { p = pionki.front(); if (plansz[p.first][p.second]) { if (test_w(p.first, p.second, plansz)) { zdjetych++; plansz[p.first][p.second] = false; if (p.first>0) pionki.push({p.first-1,p.second}); if (p.first<n) pionki.push({p.first+1,p.second}); if (p.second>0) pionki.push({p.first,p.second-1}); if (p.second<m) pionki.push({p.first,p.second+1}); } } pionki.pop(); } // print_v(plansz); return (zdjetych); } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int x,y; cin >> n >> m >> k >> q; for (int i=0; i <= n+1; i++) { plansz.push_back({}); for (int j=0; j<=m+1; j++) { plansz[i].push_back(false); } } for (int j=0; j<k; j++) { cin >> x >> y; plansz[x][y]=true; pionki.push({x,y}); } // print_v(plansz); cout << licz_z(plansz, pionki) << endl; for (int i=0; i<q; i++) { cin >> x >> y; plansz[x][y]=!plansz[x][y]; pionki.push({x,y}); // print_v(plansz); cout << licz_z(plansz, pionki) << endl; } return(0); } |
English