NOJ [1187] Hole Breaker

 
  • 问题描述
  • XadillaX wants a big hole to store some *&@#(*&@!#(*&!)(@&#^& things. So he bought a Hole Breaker.
    The ground is an N * N square. The breaker will break a 1 * 1 square hole in the ground per second.
    XadillaX is so excited that he can't wait any more. After the breaker worked for several seconds, he will calculate out the maximum hole area.

  • 输入
  • This problem contains several cases.
    The first line of each case contains two integers N and M, indicate the side of ground and the number of operations. (N <= 3 <= 1000, 1 <= M <= 200000).
    Then follow M lines.
    Each line is a command:
    B x y: Break a hole at (x, y). You can assume that every coordinate will at most break for once.
    C : Calculate out the maximum area of the holes and output. (Two holes are connected when they share an edge)
  • 输出
  • For each 'C' command, output the maximum area of the holes.

    这题就是并查集,二维当一维来做,苦逼的我查错查了很久....

    #include<stdio.h>
    #include<string.h>
    
    int father[1001000];
    int rank[1001000];
    bool is_break[1001000];
    int n,ans;
    int find(int x)
    {
    	if(x==father[x])
    	   return father[x];
    	else
    	{
    		 father[x]=find(father[x]);
             return father[x];
    	}
    	 
    }
    void Union(int x1,int x2)
    {
    	int p1=find(x1);
    	int p2=find(x2);
    	if(p1!=p2)
    	{
    		father[p2]=p1;
    		rank[p1]+=rank[p2];
    		if(rank[p1]>ans)
    	     ans=rank[p1];
    	   return ;
    	}
    }
    
    int main()
    {
    	int m;
    	while(~scanf("%d%d",&n,&m))
    	{
    		int i,j;
    		int x,y;
    		char op[5];
    		ans=0;
    		for(i=0;i<n*n;i++)
    		{
    			father[i]=i;
    			rank[i]=1;
    		} 
    		int z;
    		memset(is_break,0,sizeof(is_break));
    		int is_not=0;
    		for(i=1;i<=m;i++)
    		{
    			scanf("%s",op);
    			if(op[0]=='B')
    			{
    				scanf("%d%d",&x,&y);
                    is_not=1;
                   
                    if(x!=0)
                      z=x*n+y;
                    else 
                      z=y;
                    is_break[z]=1;
    				if( x!=0 && is_break[z-n]==1)
    					 Union(z,z-n);
                    if(x!=n-1 && is_break[z+n]==1)
    	                 Union(z,z+n);
                    if(y!=0 && is_break[z-1]==1)
                         Union(z,z-1);
                    if(y!=n-1 && is_break[z+1]==1)
                         Union(z,z+1);
    			}
    			else if(op[0]=='C')
    			{
    				if(ans==0 && is_not==1)
    			      printf("1\n");
                   else
    				printf("%d\n",ans);
    			} 
    		}
    	}
    	return 0;
    }


     

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值