usaco The Castle (并查集)

/*
PROG: castle
LANG: C++
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(i=a;i<=b;i++)
#define N 10000000
using namespace std;

struct WALL
{
    bool w,n,e,s;
}to[20],wall[55][55];

int tmp_b[4]={1,2,4,8},m,n;

void init()
{
    int i;
    for(i=0;i<(1<<4);i++)
    {
        int j=0;
        WALL tmp_w={0,0,0,0};
        int sum=0;
        while(i>>j)
        {
            if((i>>j)&1)
            {
                sum+=tmp_b[j];
                switch(tmp_b[j])
                {
                case 1:
                    tmp_w.w=1;
                    break;
                case 2:
                    tmp_w.n=1;
                    break;
                case 4:
                    tmp_w.e=1;
                    break;
                case 8:
                    tmp_w.s=1;
                    break;
                }
            }
            j++;
        }
        to[sum]=tmp_w;
        //printf("%d : %d %d %d %d\n",sum,to[sum].e,to[sum].s,to[sum].w,to[sum].n);
    }
}

int fa[3000],room_size[3000];

int root(int a)
{
    if(fa[a]==0 || fa[a]==a) return a;
    else return fa[a]=root(fa[a]);
}

void Merge(int a,int b)
{
    a=root(a);b=root(b);
    fa[a]=b;
    room_size[b]+=room_size[a];
}

inline int room_number(int i,int j)
{
    return (i-1)*m+j;
}

bool check_nxi_nxj(int i,int j)
{
    if(i>=1 && i<=n && j>=1 && j<=m) return 1;
    else return 0;
}

int main()
{
    freopen("castle.in","r",stdin);
    freopen("castle.out","w",stdout);
    int i,j,tmp;
    init();
    scanf("%d%d",&m,&n);
    rep(i,1,n) rep(j,1,m) scanf("%d",&tmp),wall[i][j]=to[tmp],room_size[room_number(i,j)]=1;
    //rep(i,1,n) rep(j,1,m) cout<<room_number(i,j)<<endl;
    rep(i,1,n)
    {
        rep(j,1,m)
        {
            if(!wall[i][j].n)
            {
                if(root(room_number(i-1,j))!=root(room_number(i,j)))
                    Merge(room_number(i-1,j),room_number(i,j));
            }
            if(!wall[i][j].s)
            {
                if(root(room_number(i+1,j))!=root(room_number(i,j)))
                    Merge(room_number(i+1,j),room_number(i,j));
            }
            if(!wall[i][j].w)
            {
                if(root(room_number(i,j-1))!=root(room_number(i,j)))
                    Merge(room_number(i,j-1),room_number(i,j));
            }
            if(!wall[i][j].e)
            {
                if(root(room_number(i,j+1))!=root(room_number(i,j)))
                    Merge(room_number(i,j+1),room_number(i,j));
            }
        }
    }
    int how_many_room=0,largest_room_size=1,ct=0,largest_possible=1,ansi,ansj;
    char answ;
    rep(i,1,n) rep(j,1,m)
    {
        ct++;
        //if(i==4 && j==1) printf("%d\n",room_size[root(ct)]+room_size[root(room_number(i-1,j))]);
            //printf("%d\n",wall[i-1][j].s);
        if(root(room_number(i,j))==room_number(i,j)) how_many_room++;
        //printf("%d\n",room_size[root(room_number(i,j))]);
        largest_room_size=max(largest_room_size,room_size[root(room_number(i,j))]);
        int nxi,nxj;
        if(wall[i][j].n)
        {
            nxi=i-1,nxj=j;
            if(check_nxi_nxj(nxi,nxj) && root(ct)!=root(room_number(nxi,nxj)))
            {
                //cout<<"haha"<<endl;
                //largest_possible=max(largest_possible,room_size[root(ct)]+room_size[root(room_number(nxi,nxj))]);
                if(largest_possible<room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    largest_possible=room_size[root(ct)]+room_size[root(room_number(nxi,nxj))];
                    ansi=i;ansj=j;
                    answ='N';
                }
                else if(largest_possible==room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    if(j<ansj) ansj=j,ansi=i,answ='N';
                    else if(j==ansj)
                    {
                        if(ansi<i) ansi=i,answ='N';
                    }
                }
            }
        }
        if(wall[i][j].s)
        {
            nxi=i+1,nxj=j;
            if(check_nxi_nxj(nxi,nxj) && root(ct)!=root(room_number(nxi,nxj)))
            {
                //largest_possible=max(largest_possible,room_size[root(ct)]+room_size[root(room_number(nxi,nxj))]);
                if(largest_possible<room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    largest_possible=room_size[root(ct)]+room_size[root(room_number(nxi,nxj))];
                    ansi=i;ansj=j;
                }
                else if(largest_possible==room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    if(j<ansj) ansj=j,ansi=i;
                    else if(j==ansj)
                    {
                        if(ansi<i) ansi=i;
                    }
                }
            }
        }
        if(wall[i][j].w)
        {
            nxi=i,nxj=j-1;
            if(check_nxi_nxj(nxi,nxj) && root(ct)!=root(room_number(nxi,nxj)))
            {
                //largest_possible=max(largest_possible,room_size[root(ct)]+room_size[root(room_number(nxi,nxj))]);
                if(largest_possible<room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    largest_possible=room_size[root(ct)]+room_size[root(room_number(nxi,nxj))];
                    ansi=i;ansj=j;
                }
                else if(largest_possible==room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    if(j<ansj) ansj=j,ansi=i;
                    else if(j==ansj)
                    {
                        if(ansi<i) ansi=i;
                    }
                }
            }
        }
        if(wall[i][j].e)
        {
            nxi=i,nxj=j+1;
            if(check_nxi_nxj(nxi,nxj) && root(ct)!=root(room_number(nxi,nxj)))
            {
                //largest_possible=max(largest_possible,room_size[root(ct)]+room_size[root(room_number(nxi,nxj))]);
                if(largest_possible<room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    largest_possible=room_size[root(ct)]+room_size[root(room_number(nxi,nxj))];
                    ansi=i;ansj=j;
                    answ='E';
                }
                else if(largest_possible==room_size[root(ct)]+room_size[root(room_number(nxi,nxj))])
                {
                    if(j<ansj) ansj=j,ansi=i,answ='E';
                    else if(j==ansj)
                    {
                        if(ansi<i) ansi=i,answ='E';
                    }
                }
            }
        }
    }
    printf("%d\n%d\n%d\n%d %d %c\n",how_many_room,largest_room_size,largest_possible,ansi,ansj,answ);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值