http://acm.hdu.edu.cn/showproblem.php?pid=1198
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 4000 5 using namespace std; 6 7 int b[11][4]={{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,1},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}}; 8 char g[60][60]; 9 int n,m; 10 int f[maxn]; 11 12 int Find(int x) 13 { 14 if(x==f[x]) return x; 15 return f[x]=Find(f[x]); 16 } 17 18 void inti() 19 { 20 for(int i=0; i<=n*m+1; i++) f[i]=i; 21 } 22 23 void union1(int a,int b) 24 { 25 int fa=Find(a); 26 int fb=Find(b); 27 if(fa!=fb) 28 f[fa]=fb; 29 } 30 int main() 31 { 32 while(scanf("%d%d",&n,&m)!=EOF) 33 { 34 inti(); 35 if(n==-1&&m==-1) break; 36 for(int i=0; i<n; i++) 37 { 38 scanf("%s",g[i]); 39 } 40 for(int i=0; i<n; i++) 41 { 42 for(int j=0; j<m; j++) 43 { 44 if(b[g[i][j]-'A'][3]==1&&b[g[i][j+1]-'A'][2]==1&&j+1<m) 45 { 46 union1(i*m+j,i*m+j+1); 47 } 48 if(b[g[i][j]-'A'][1]==1&&b[g[i+1][j]-'A'][0]==1&&i+1<n) 49 { 50 union1(i*m+j,(i+1)*m+j); 51 } 52 } 53 } 54 int cnt=0; 55 for(int i=0; i<n*m; i++) 56 { 57 if(f[i]==i) cnt++; 58 } 59 printf("%d ",cnt); 60 } 61 return 0; 62 }