NOJ [1187] Hole Breaker

来源:互联网 发布:matlab读取字符串数组 编辑:程序博客网 时间:2024/05/18 01:40
 
  • 问题描述
  • 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");               elseprintf("%d\n",ans);} }}return 0;}


     

 

0 0
原创粉丝点击