HDU5925-Coconuts

来源:互联网 发布:淘宝网商银行存钱吗 编辑:程序博客网 时间:2024/06/08 07:56

Coconuts

                                                                     Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                             Total Submission(s): 1006    Accepted Submission(s): 304


Problem Description
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).
 

Input
The first line contains apositiveinteger T(T10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0<R,C109 the second line contains an integer n, the number of bad coconuts, 0n200 from the third line, there comes n lines, each line contains two integers, xi and yi, which means in cell(xi,yi), there is a bad coconut.

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.
 

Output
For each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.
 

Sample Input
23 321 22 13 312 2
 

Sample Output
Case #1:21 6Case #2:18
 

Source
2016CCPC东北地区大学生程序设计竞赛 - 重现赛
 

Recommend
wange2014
 

题意:有r*c的方阵 上面都是白的,其中有n个黑色的点 ,问黑色的点把白色的点分成了几个部分,每个部分的点有多少

解题思路:黑点很少,只有200个,空白的区域很大。可以进行离散化,离散化后图最坏也只是400*400,这样的话搜索就不会超时了。要离散的是X轴和Y轴,将障碍点输入以后,分x轴,y轴数组存储,然后再进行排序,利用一个点和前一个点的距离来判断是否有可压缩的点,所以0和边界都要算。然后dfs的时候调用压缩的数据就好了


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n, w, h, cnt;LL a[709],b[709],aa[709], bb[709];int mp[709][709];LL sum[709],res,sumx, sumy,r, c;int visx[709], visy[709],vis[709][709];int dir[4][2] = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };LL Abs(LL x){if (x < 0) return -x;return x;}int f(LL *x,LL *xx, LL w){vector<LL>g;g.push_back(0);g.push_back(w - 1);for (int i = 0; i < n; i++)for (int j = -1; j <= 1; j++){LL k = x[i] + j;if (k >= 0 && k < w) g.push_back(k);}sort(g.begin(), g.end());g.erase(unique(g.begin(), g.end()), g.end());for (int i = 0; i < n; i++) x[i] = find(g.begin(), g.end(), x[i]) - g.begin();for (int i = 0; i < g.size(); i++) xx[i] = g[i];return g.size();}void dfs(int x, int y){vis[x][y] = 1;for (int i = 0; i < 4; i++){int xx = x + dir[i][0];int yy = y + dir[i][1];if (vis[xx][yy] || mp[xx][yy] || xx<0 || yy<0 || xx >= w || yy >= h) continue;if (Abs(aa[x] - aa[xx]) > 1 && visx[x] * visx[xx] == 0){visx[x] = visx[xx] = 1;sumx += Abs(aa[x] - aa[xx]) - 1;}if (Abs(bb[y] - bb[yy]) > 1 && visy[y] * visy[yy] == 0){visy[y] = visy[yy] = 1;sumy += Abs(bb[y] - bb[yy]) - 1;}res++;dfs(xx, yy);}}int main(){int t, cas = 0;scanf("%d", &t);while (t--){scanf("%lld%lld", &r, &c);scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%lld%lld", &a[i], &b[i]);a[i]--;b[i]--;}w = f(a, aa, r);h = f(b, bb, c);memset(mp, 0, sizeof(mp));for (int i = 0; i < n; i++) mp[a[i]][b[i]] = 1;memset(vis, 0, sizeof vis);memset(sum, 0, sizeof sum);cnt = 0;for (int i = 0; i < w; i++){for (int j = 0; j < h; j++){if (vis[i][j] == 1 || mp[i][j] == 1) continue;sumx = sumy = 0;res = 1;memset(visx, 0, sizeof visx);memset(visy, 0, sizeof visy);dfs(i, j);sum[cnt++] = res - sumx * sumy + sumx * c + sumy * r;}}sort(sum, sum + cnt);printf("Case #%d:\n", ++cas);printf("%d\n", cnt);for (int i = 0; i < cnt; i++){printf("%lld", sum[i]);if (i != cnt - 1) printf(" ");else printf("\n");}}return 0;}

原创粉丝点击