/* And query the set! Finally, several fathers are the answer. */ # Include < Iostream >
# Include < Set >
Using Namespace STD;
Struct G {
Int Left;
Int Right;
Int Up;
Int Down;
};
Const Int Maxn = 50 ;
Int Father [maxn * Maxn];
Int M, N;
G [ 11 ] = {{ 1 , 0 , 1 , 0 },{ 0 , 1 , 1 , 0 },{ 1 , 0 , 0 , 1 },
{ 0 , 1 , 0 , 1 },{ 0 , 0 , 1 , 1 },{ 1 , 1 , 0 , 0 },{ 1 , 1 , 1 , 0 },
{ 1 , 0 , 1 , 1 },{ 1 , 1 , 0 , 1 },{ 0 , 1 , 1 , 1 },{ 1 , 1 , 1 , 1 }};
Void Markset ( Int M, Int N ){
For ( Int I = 0 ; I < M * N; I ++ ){
Father [I] = I;
}
}
Int Findset ( Int X ){
If (Father [x] ! = X)
Father [x] = Findset (father [x]);
Return Father [x];
}
Void Merge ( Int X1, Int Y1, Int X2, Int Y2 ){
Int FX = X1 * N + Y1; //
Int FY = X2 * N + Y2; //
FX = Findset (FX );
FY = Findset (FY );
If (FX = FY ){
Return ;
} Else If (FX < FY ){
Father [FY] = FX;
} Else {
Father [FX] = FY;
}
}
Bool Connect1 ( Char Success, Char CH2 ){
If (G [Signature - ' A ' ]. Right = 1 && G [CH2 - ' A ' ]. Left = 1 )
Return True ;
Else
Return False ;
}
Bool Connect2 ( Char Success, Char CH2 ){
If (G [Signature - ' A ' ]. Down = 1 && G [CH2 - ' A ' ]. Up = 1 )
Return True ;
Else
Return False ;
}
Int Main (){
Char Map [maxn] [maxn];
Set < Int > S;
While (CIN > M > N ){
If (M < 0 && N < 0 )
Break ;
S. Clear ();
Markset (m, n );
For ( Int I = 0 ; I < M; I ++ ){
CIN > Map [I] [ 0 ];
If (I ! = 0 && Connect2 (Map [I - 1 ] [ 0 ], Map [I] [ 0 ]) {
Merge (I, 0 , I - 1 , 0 );
}
For ( Int J = 1 ; J < N; j ++ ){
CIN > Map [I] [J];
If (Connect1 (Map [I] [J - 1 ], Map [I] [J])
Merge (I, j - 1 , I, j );
If (I ! = 0 && Connect2 (Map [I - 1 ] [J], map [I] [J]) {
Merge (I - 1 , J, I, j );
}
}
}
For ( Int I = 0 ; I < M * N; I ++ )
Findset (I );
For ( Int I = 0 ; I < M * N; I ++ )
S. insert (father [I]);
Cout < S. Size () < Endl; // Use the STL set container to determine whether it is connected.
}
Return 0 ;
}