Coconuts HDU - 5925 (离散化+dfs求联通块)

来源:互联网 发布:gta5您必须有网际网络 编辑:程序博客网 时间:2024/06/05 06:32

http://acm.hdu.edu.cn/showproblem.php?pid=5925

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(T10T≤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,C1090<R,C≤109 the second line contains an integer n, the number of bad coconuts, 0n2000≤n≤200 from the third line, there comes n lines, each line contains two integers, xixi and yiyi, which means in cell(xi,yixi,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
题目大意: R行C列的网格(R,C<=109),有N(N<=200)个障碍,求联通块数量和各个大小(从小到大)。

ps:吐槽一下,这个题做了整整一下午了,看了无数的题解可是都没有自己想要的那个思路,最终还是自己根据自己思路把它写出来了,

题解:因为图太大,而障碍点少那么就讲非障碍的地方缩成一个点,一定要对齐,凡是有x,y的就画垂直水平线,然后所得出来的小矩阵成为点,注意:在线上的点特殊考虑,有水平垂直线交点的点也要特殊处理,判断一下是不是障碍点(此处用的离散化后的标记识别),建出新图来以后dfs求联通块就可以了

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <map>using namespace std;long long  x[222],y[222];long long A[444][444];int B[444][444];int cnt;int R,C;int dx[]= {1,-1,0,0};int dy[]= {0,0,1,-1};long long sum;long long d[222222];map<long long ,long long >mx,my;struct node{    long long x;    long long y;} p[222];int ma[422][422];void dfs(int x,int y,int k){    if(x<0||y<0||x>=R||y>=C)return ;    if(B[x][y]>0||A[x][y]==0)return ;    B[x][y]=k;    sum+=A[x][y];    for(int i=0; i<4; i++)    {        int nowx=x+dx[i];        int nowy=y+dy[i];        dfs(nowx,nowy,k);    }    return ;}bool cmp(node a,node b){    return a.x<b.x;}bool cmp1(node a,node b){    return a.y<b.y;}int main(){    int t;    scanf("%d",&t);    int w=1;    while(t--)    {        ///初始化        long long r,c;        int n,m;        scanf("%lld%lld",&r,&c);        scanf("%d",&n);        x[0]=y[0]=0;        mx.clear();        my.clear();        memset(ma,0,sizeof(ma));        for(int i=1; i<=n; i++)        {            scanf("%lld%lld",&p[i].x,&p[i].y);            x[i]=p[i].x;            y[i]=p[i].y;        }        ///为了标记障碍点进行离散化        sort(p+1,p+1+n,cmp);        int pre=1;        mx[p[1].x]=pre++;        for(int i=2; i<=n; i++)        {            if(p[i].x!=p[i-1].x)                mx[p[i].x]=pre++;        }        sort(p+1,p+1+n,cmp1);        pre=1;        my[p[1].y]=pre++;        ma[mx[p[1].x]][my[p[1].y]]=1;        for(int i=2; i<=n; i++)        {            if(p[i].y!=p[i-1].y)                my[p[i].y]=pre++;            ma[mx[p[i].x]][my[p[i].y]]=1;        }        ///建新图,缩图        sort(x,x+n+1);        sort(y,y+n+1);        int nn=n;        n=unique(x,x+nn+1)-x;        m=unique(y,y+nn+1)-y;//        for(int i=0; i<n; i++)cout<<x[i]<<" ";//        cout<<endl<<"*";//        for(int i=0; i<m; i++)cout<<y[i]<<" ";//        cout<<endl;        int sr,sc;        sr=sc=0;        for(int i=1; i<n; i++)        {            if(x[i]-x[i-1]>1)            {                long long xx=x[i]-x[i-1]-1;                for(int j=1; j<m; j++)                {                    if(y[j]-y[j-1]>1)A[sr][sc++]=(y[j]-y[j-1]-1)*xx;                    A[sr][sc++]=xx;                }                if(y[m-1]!=c)A[sr][sc++]=(c-y[m-1])*xx;                sr++;            }            C=sc;            sc=0;            for(int j=1; j<m; j++)            {                if(y[j]-y[j-1]>1)A[sr][sc++]=y[j]-y[j-1]-1;                if(ma[mx[x[i]]][my[y[j]]])A[sr][sc++]=0;                else A[sr][sc++]=1;            }            if(y[m-1]!=c)A[sr][sc++]=c-y[m-1];            sr++;            C=sc;            sc=0;        }        if(x[n-1]!=r)        {            long long xx=r-x[n-1];            for(int j=1; j<m; j++)            {                if(y[j]-y[j-1]>1)A[sr][sc++]=(y[j]-y[j-1]-1)*xx;                A[sr][sc++]=xx;            }            if(y[m-1]!=c)A[sr][sc++]=(c-y[m-1])*xx;            sr++;        }        R=sr;//        for(int i=0; i<R; i++)//        {//            for(int j=0; j<C; j++)//            {//                printf("%d ",A[i][j]);//            }//            printf("\n");//        }///求联通块        memset(B,0,sizeof(B));        cnt=0;        for(int i=0; i<R; i++)        {            for(int j=0; j<C; j++)            {                if(A[i][j]!=0&&B[i][j]==0)                {                    sum=0;///记录联通块的大小                    dfs(i,j,++cnt);                    d[cnt]=sum;                }            }        }        sort(d,d+1+cnt);        printf("Case #%d:\n",w++);        if(cnt==0)            printf("%d\n",cnt);        else        {            printf("%d\n",cnt);            for(int i=1; i<=cnt; i++)            {                if(i==1)printf("%lld",d[i]);                else printf(" %lld",d[i]);            }            printf("\n");        }    }    return 0;}



0 0