HDU5925:Coconuts(二维坐标离散化 + DFS)

Coconuts

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


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
  
  
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
题意:给一个R*C的图,和若干个障碍,问连通块的个数及其面积。

思路:图的范围很大,但障碍点的数量很少,需要先离散化,具体就是将x和y坐标分开离散化,将各个“障碍包围圈”之间的距离缩成一个格子,这样就能保持“障碍圈”的形状不变了。最后dfs下,每个格子的面积分别计算就行。参考http://www.cnblogs.com/general10/p/6344956.html?utm_source=itdadao&utm_medium=referral

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<algorithm>
#define cl(a,b) memset(a,b,sizeof(a))
#define debug(x) cerr<<#x<<"=="<<(x)<<endl
using namespace std;
typedef long long ll;

const int maxn=2e2+10;

int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};

ll ans[maxn*maxn],sum;
int n,m,k,cntx,cnty;
int x[maxn],y[maxn];
int xx[maxn],yy[maxn];
int linex[maxn],liney[maxn];
bool vis[maxn][maxn];
map<int,int>ux,uy;

void init()
{
    cntx=0,cnty=0;
    cl(ans,0),cl(vis,0);
    ux.clear(),uy.clear();
}

bool judge(int x,int y)
{
    if(x<1||x>cntx||y<1||y>cnty||vis[x][y]) return false;
    return true;
}

void dfs(int x,int y)
{
    if(!judge(x,y)) return ;
    vis[x][y]=true;
    sum+=1ll*linex[x]*liney[y];
    for(int i=0;i<4;i++)
        dfs(x+dx[i],y+dy[i]);
}

int main()
{
    int cas=1,T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d%d",&n,&m,&k);
        int nx=0,ny=0;
        xx[++nx]=1,xx[++nx]=n;
        yy[++ny]=1,yy[++ny]=m;
        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            xx[++nx]=x[i],yy[++ny]=y[i];
        }
        sort(xx+1,xx+nx+1);
        nx=unique(xx+1,xx+nx+1)-(xx+1);
        for(int i=1;i<=nx;i++)
        {
            if(xx[i]!=xx[i-1]+1) linex[++cntx]=xx[i]-(xx[i-1]+1);
            linex[++cntx]=1;
            ux[xx[i]]=cntx;
        }
        sort(yy+1,yy+ny+1);
        ny=unique(yy+1,yy+ny+1)-(yy+1);
        for(int i=1;i<=ny;i++)
        {
            if(yy[i]!=yy[i-1]+1) liney[++cnty]=yy[i]-(yy[i-1]+1);
            liney[++cnty]=1;
            uy[yy[i]]=cnty;
        }
        for(int i=1;i<=k;i++) vis[ux[x[i]]][uy[y[i]]]=true;
        int cnt=0;
        for(int i=1;i<=cntx;i++)
        {
            for(int j=1;j<=cnty;j++)
            {
                if(!vis[i][j])
                {
                    sum=0;
                    dfs(i,j);
                    ans[cnt++]=sum;
                }
            }
        }
        sort(ans,ans+cnt);
        printf("Case #%d:\n",cas++);
        printf("%d\n",cnt);
        for(int i=0;i<cnt;i++)
            printf("%lld%c",ans[i],i==cnt-1?'\n':' ');
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值