UVA_10651
由于总状态很少,所以直接广搜即可。
#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;
}