http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=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.
  • 样例输入
  • 3 5
    B 0 0
    C
    B 1 0
    B 2 2
    C
    
  • 样例输出
  • 1
    2
    
    题意:求在当前操作下最大的洞的个数。
    思路:并查集的应用
    AC代码:
    #include<iostream>
    #include<string.h>
    #include<string>
    #include<cstdio>
    #define N 1000005
    using namespace std;
    struct node
    {
    	int Num;
    	bool flag;
    }map[1005][1005];
    int Father[N],num[N];
    typedef struct node Node;
    void init(int n)
    {
    	
    	int res=0;
    	for(int i=0;i<n;++i)
    		for(int j=0;j<n;++j)
    			map[i][j].flag=false,map[i][j].Num=res++;
    	 for(int i=0;i<n*n;++i)
    		 Father[i]=i,num[i]=0;
    }
    int Find(int x)
    {
    	if(Father[x]==x) return x;
    	else return Father[x]=Find(Father[x]);
    }
    void Union(int a,int b,int& c)
    {
    	int xx=Find(a);
    	int yy=Find(b);
    	if(xx!=yy)
    	{
    		Father[xx]=yy;
    		num[yy]+=num[xx];
    		if(num[yy]>c) c=num[yy];
    	}
    }
    int main()
    {
    	int n,m;
    	while(~scanf("%d%d",&n,&m))
    	{
    		int minx=0;
    		init(n);
    		char ch[10];
    		for(int i=0;i!=m;++i)
    		{
    			scanf("%s",ch);
    			if(ch[0]=='B')
    			{
    				int a,b;
    			   scanf("%d%d",&a,&b);
    				map[a][b].flag=true;
    				num[map[a][b].Num]=1;
    				if(num[map[a][b].Num]>minx) minx=num[map[a][b].Num];
    				if(a-1>=0&&a-1<n&&b>=0&&b<n&&map[a-1][b].flag) Union(map[a][b].Num,map[a-1][b].Num,minx);
    				if(a+1>=0&&a+1<n&&b>=0&&b<n&&map[a+1][b].flag) Union(map[a][b].Num,map[a+1][b].Num,minx);
    				if(a>=0&&a<n&&b-1>=0&&b-1<n&&map[a][b-1].flag) Union(map[a][b].Num,map[a][b-1].Num,minx);
    				if(a>=0&&a<n&&b+1>=0&&b+1<n&&map[a][b+1].flag) Union(map[a][b].Num,map[a][b+1].Num,minx);
    			}
    			else if(ch[0]=='C') printf("%d\n",minx);
    		}
    	}return 0;
    
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值