这题挺好玩的。。。。。并查集。。。
#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;
}