TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1).
Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component).
Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component).
It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time.
2 3 3 2 1 2 2 1 3 3 1 2 2
Case #1: 2 1 6 Case #2: 18
在一张很大的图里放几个点分隔,问连通快的数量及大小
#include<map> #include<queue> #include<cstdio> #include<algorithm> using namespace std; typedef pair<int,int> pii; const int N = 2e5 + 10; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int low(int x) { return x&-x; } int T, n, r, c, t; int a[N], b[N]; long long d[N]; map<pii,int> M, Q, P; int dir[8][2] = {0,1,0,-1,1,0,-1,0,1,1,-1,-1,1,-1,-1,1}; void bfs(int x,int y) { queue<pii> p; t = 0; p.push(make_pair(x,y)); P.clear(); P[make_pair(x,y)] = 1; while (!p.empty()) { pii q = p.front(); p.pop(); t++; if (t + t > n*n) break; for (int i = 0;i < 4;i++) { int X = q.first + dir[i][0]; int Y = q.second + dir[i][1]; if (X < 1 || X > r || Y < 1 || Y > c) continue; if (M.count(make_pair(X,Y))) continue; if (P.count(make_pair(X,Y))) continue; if (Q.count(make_pair(X,Y))) { t = mod; return; } p.push(make_pair(X,Y)); P[make_pair(X,Y)] = 1; } } while (!p.empty()) p.pop(); for (map<pii,int>::iterator it = P.begin();it!=P.end();it++) { pii u = it->first; Q[u] = 1; } } int main(){ int cas = 1; for (scanf("%d",&T);T--;cas++) { scanf("%d%d%d",&r, &c, &n); M.clear(); Q.clear(); int sz = 0; for (int i = 1;i<=n;i++) { scanf("%d%d",&a[i], &b[i]); M[make_pair(a[i],b[i])] = 1; } for (int i = 1;i <= n;i++) { for (int j = 0;j < 8;j++) { int x = a[i] + dir[j][0]; int y = b[i] + dir[j][1]; if (M.count(make_pair(x, y))) continue; if (Q.count(make_pair(x, y))) continue; if (x < 1 || x > r || y < 1 || y > c) continue; bfs(x, y); if (t + t <= n*n) d[sz++] = t; } } d[sz] = 1LL * r * c - n; for (int i = 0;i<sz;i++) { d[sz] -= d[i]; } if (d[sz]) sz++; sort(d,d+sz); printf("Case #%d:\n",cas); printf("%d\n",sz); for (int i = 0;i<sz;i++) { printf("%lld%s",d[i],i==sz-1?"\n":" "); } } return 0; }