hdu 1198 并查集

这题挺好玩的。。。。。并查集。。。

#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
char a[55][55];
int f[3000];
int n,m;
string k[11]={"1001","1100","0011","0110","1010","0101","1101","1011","0111","1110","1111"};
int find(int x)
{
   if(x==f[x])
        return x;
    else
        return f[x]=find(f[x]);

}
int same(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y)
        return 1;
    else
        return 0;
}
void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    f[x]=y;
}
void judge(int x,int y)
{
    if(x-1>=0&&(k[a[x-1][y]-'A'][2]=='1')&&(k[a[x][y]-'A'][0]=='1'))
        unite(x*m+y,(x-1)*m+y);
    if(y-1>=0&&(k[a[x][y-1]-'A'][1]=='1')&&(k[a[x][y]-'A'][3]=='1'))
        unite(x*m+y,x*m+y-1);
}
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    map<int,int> v;
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF){
            if(n==-1&&m==-1)
                break;
    v.clear();
    for(i=0;i<n*m;i++)
        f[i]=i;
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>a[i][j];
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            judge(i,j);
    for(i=0;i<n*m;i++)
    {
        v[find(i)]=1;
    }
    printf("%d\n",v.size());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值