Uva_000051
Because the total status is very small, you can directly search for it.
# Include <stdio. h>
# Include < String . H>
# Define Maxd 10010
Char B [ 20 ];
Int D [maxd], Q [maxd], hash [maxd], N;
Void Init ()
{
Int I, J;
Scanf ( " % S " , B );
N = Q [ 0 ] = 0 ;
For (I = 0 ; I < 12 ; I ++)
{
Q [ 0 ] = (Q [ 0 ] < 1 ) + (B [I] = ' O ' ? (N ++, 1 ): 0 );
}
}
Void Solve ()
{
Int I, J, K, X, front, rear;
Front = rear = 0 ;
Memset (hash, 0 , Sizeof (Hash ));
Hash [Q [rear] =1 ;
D [rear] = 0 ;
Rear ++;
While (Front <rear)
{
X = Q [Front];
For (I = 0 ; I < 12 ; I ++)
If (( 1 <I) & X)
{
If (I <10 &&(( 1 <(I + 1 ) & X )&&! (( 1 <(I + 2 ) & X ))
{
Q [rear] = x ^ ( 1 <I) ^ ( 1 <(I + 1 ) ^ ( 1 <(I + 2 ));
If (! Hash [Q [rear])
{
D [rear] = d [Front] + 1 ;
Hash [Q [rear] = 1 ;
Rear ++;
}
}
If (I> 1 &&(( 1 <(I- 1 ) & X )&&! (( 1 <(I-2 ) & X ))
{
Q [rear] = x ^ ( 1 <I) ^ ( 1 <(I- 1 ) ^ ( 1 <(I- 2 ));
If (! Hash [Q [rear])
{
D [rear] = d [Front] + 1 ;
Hash [Q [rear] =1 ;
Rear ++;
}
}
}
Front ++;
}
Printf ( " % D \ n " , N-d [rear- 1 ]);
}
Int Main ()
{
Int T;
Scanf (" % D " , & T );
While (T --)
{
Init ();
Solve ();
}
Return 0 ;
}