hdu 5925 Coconuts 离散化+dfs
Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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)
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 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 y, which means in cell(xi,y), 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
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e3+500,M=4e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; int n,m,q; struct point { int x,y; }a[N]; ll l[N]; int getpos(int x,int flag) { return lower_bound(l,l+flag,x)-l; } int mp[N][N]; int vis[N][N]; int xx[4]={0,1,0,-1}; int yy[4]={-1,0,1,0}; int flag; int nn,mm; int check(int x,int y) { if(x<0||x>nn||y<0||y>mm) return 0; return 1; } void dfs(int n,int m,int deep) { vis[n][m]=deep; for(int i=0;i<4;i++) { int xxx=n+xx[i]; int yyy=m+yy[i]; if(check(xxx,yyy)&&!mp[xxx][yyy]&&!vis[xxx][yyy]) { dfs(xxx,yyy,deep); } } } ll ans[100010]; ll getnum(int x) { if(x==0) return l[x]; return l[x]-l[x-1]; } int main() { int T,cas=1; scanf("%d",&T); while(T--) { flag=0; memset(mp,0,sizeof(mp)); memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); int kuai=1; scanf("%d%d",&n,&m); scanf("%d",&q); for(int i=1;i<=q;i++) { scanf("%d%d",&a[i].x,&a[i].y); l[flag++]=a[i].x; l[flag++]=a[i].y; if(a[i].x+1<=n) l[flag++]=a[i].x+1; if(a[i].y+1<=m) l[flag++]=a[i].y+1; if(a[i].x-1) l[flag++]=a[i].x-1; if(a[i].y-1) l[flag++]=a[i].y-1; } l[flag++]=1; if(n>=2||m>=2) l[flag++]=2; l[flag++]=n; l[flag++]=m; sort(l,l+flag); flag=unique(l,l+flag)-l; for(int i=1;i<=q;i++) { mp[getpos(a[i].x,flag)][getpos(a[i].y,flag)]=1; } nn=getpos(n,flag); mm=getpos(m,flag); for(int i=0;i<=nn;i++) { for(int t=0;t<=mm;t++) { if(!mp[i][t]&&!vis[i][t]) { dfs(i,t,kuai++); } } } for(int i=0;i<=nn;i++) { for(int t=0;t<=mm;t++) { if(vis[i][t]) { ans[vis[i][t]]+=getnum(i)*getnum(t); } } } printf("Case #%d:\n",cas++); printf("%d\n",kuai-1); if(kuai-1) { sort(ans+1,ans+kuai); printf("%lld",ans[1]); for(int i=2;i<kuai;i++) printf(" %lld",ans[i]); printf("\n"); } } return 0; }