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.
For each 'C' command, output the maximum area of the holes.
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;
}