/*
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;
}
usaco The Castle (并查集)
最新推荐文章于 2021-01-18 16:26:37 发布