Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 762 Accepted Submission(s): 223
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).
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(
T≤10
) 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,C≤109
the second line contains an integer n, the number of bad coconuts,
0≤n≤200
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.
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
2 3 3 2 1 2 2 1 3 3 1 2 2
Sample Output
Case #1: 2 1 6 Case #2: 1 8
Source
题意:求联通块的个数 由于数据太大但是障碍点较少,所以必须离散化然后dfs遍历求连通块就好
#include <bits/stdc++.h>
using namespace std;
#define maxn 1100
int x[maxn],y[maxn];
int k[maxn];
int vis[maxn][maxn];
int disx[maxn],disy[maxn];
int pos[maxn];
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
long long num;
int nr, nc;
long long a[maxn];
void dfs( int x, int y)
{
//printf("--%d--%d--%d---%d\n",x,y,disx[x],disy[y]);
num += (long long)disx[x]*disy[y] ;
vis[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx > 0 && ny > 0 && nx <= nr && ny <= nc && !vis[nx][ny])
dfs(nx, ny);
}
}
int main()
{
int r, c,n;
int t;
int cas = 1;
scanf("%d",&t);
while(t--)
{
memset(vis, 0, sizeof(vis));
scanf("%d%d",&r,&c);
scanf("%d",&n);
int cnt = 0;
k[cnt++] = 0;
k[cnt++] = r;
for(int i = 0; i < n; i++)
{
scanf("%d%d",&x[i],&y[i]);
k[cnt++] = x[i];
}
sort(k, k+cnt);
cnt = unique(k, k + cnt) - k;
nr = 0;
for(int i = 1; i < cnt; i++)
{
if(k[i] - k[i-1] > 1)
disx[++nr] = k[i] - k[i-1] - 1;
disx[++nr] = 1;
pos[i] = nr;
}
for(int i = 0; i < n; i++)
{
int t = lower_bound(k, k + cnt, x[i]) - k;
x[i] = pos[t];
}
cnt = 0;
k[cnt++] = 0;
k[cnt++] = c;
for(int i = 0; i < n; i++)
{
k[cnt++] = y[i];
}
sort(k, k +cnt);
cnt = unique(k,k+cnt) - k;
nc = 0;
for(int i = 1; i < cnt; i++)
{
if(k[i] - k[i-1] > 1)
disy[++nc] = k[i] - k[i-1] - 1;
disy[++nc] = 1;
pos[i] = nc;
}
for(int i = 0; i < n; i++)
{
int t = lower_bound(k,k+cnt,y[i]) - k;
y[i] = pos[t];
}
for(int i = 0; i < n ; i++)
{
vis[x[i]][y[i]] = 1;
}
int ans = 0;
for(int i = 1; i <= nr; i++)
for(int j = 1; j <= nc; j++)
{
if(!vis[i][j])
{
num = 0;
dfs(i,j);
a[ans++] = num;
}
}
printf("Case #%d:\n",cas++);
printf("%d\n",ans);
sort(a,a+ans);
for(int i = 0; i < ans; i++)
{
if(i) printf(" ");
printf("%lld",a[i]);
}
printf("\n");
}
}