Coconuts HDU - 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 ( 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   a n d   C , 0 < R , C 10 9 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.
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
想法:想到了离散化,但是不知道怎么统计连通快的大小,后来知道只要赋予每个边一个权值就好了。。。
代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 100005
using namespace std;

struct node
{
    int x,y;
}p[205];
int xx[205],yy[205];
int idx[405],idy[405];
int wx[405],wy[405];
int N,M;
int getX(int x)
{
    int l=1,r=N;
    int mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(idx[mid]==x)
            return mid;
        if(idx[mid]<x)
            l=mid+1;
        else
            r=mid-1;
    }
}
int getY(int y)
{
    int l=1,r=M;
    int mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(idy[mid]==y)
            return mid;
        if(idy[mid]<y)
            l=mid+1;
        else
            r=mid-1;
    }

}
int mapp[405][405];
long long total;
int b[][2]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y)
{
    total+=(long long)wx[x]*wy[y];
    mapp[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int x1=x+b[i][0];
        int y1=y+b[i][1];
        if(x1<1||x1>N||y1<1||y1>M||mapp[x1][y1])
            continue;
        dfs(x1,y1);
    }
}
vector<long long> ans;
int main()
{
    int t;
    cin>>t;
    int cal=1;
    while(t--)
    {
        printf("Case #%d:\n",cal++);
        int n,m;
        cin>>n>>m;
        int num;
        cin>>num;
        if(!num)
        {
            cout<<1<<endl;
            cout<<(long long)n*m<<endl;
            continue;
        }
        for(int i=0;i<num;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            xx[i]=p[i].x,yy[i]=p[i].y;
        }
        int num1,num2;
        num1=num2=num;
        xx[num1++]=1,xx[num1++]=n;
        yy[num2++]=1,yy[num2++]=m;
        sort(xx,xx+num1);
        sort(yy,yy+num2);
        num1=unique(xx,xx+num1)-xx;
        num2=unique(yy,yy+num2)-yy;
        N=0;
        idx[++N]=xx[0];
        wx[N]=1;
        for(int i=1;i<num1;i++)
        {
            if(xx[i-1]+1!=xx[i])
                idx[++N]=xx[i-1]+1,wx[N]=xx[i]-xx[i-1]-1;
            idx[++N]=xx[i];
            wx[N]=1;
        }//离散化x
        M=0;
        idy[++M]=yy[0];
        wy[M]=1;
        for(int i=1;i<num2;i++)
        {
            if(yy[i-1]+1!=yy[i])
                idy[++M]=yy[i-1]+1,wy[M]=yy[i]-yy[i-1]-1;
            idy[++M]=yy[i];
            wy[M]=1;
        }//离散化y
        memset(mapp,0,sizeof(mapp));
        for(int i=0;i<num;i++)
            mapp[getX(p[i].x)][getY(p[i].y)]=1;//在离散化后的图里标记坏的coco
        ans.clear();
        for(int i=1;i<=N;i++)
            for(int j=1;j<=M;j++)//然后就正常找连通快就行了
            {
                if(mapp[i][j])
                    continue;
                total=0;
                dfs(i,j);
                ans.push_back(total);
            }
        cout<<ans.size()<<endl;
        if(ans.size())
        {
            sort(ans.begin(),ans.end());
            for(int i=0;i<ans.size()-1;i++)
                cout<<ans[i]<<" ";
            cout<<ans[ans.size()-1]<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coldfresh/article/details/80165928