UVA 10651 - Pebble Solitaire

这道题是模仿别人的bfs,却也有所收获。把12个棋位的有无棋子整体看作一个状态,然后宽搜就可以了。

代码如下:

#include<stdio.h>
#include<string.h>
#define MAXN 11000
char b[20];
int q[MAXN], hash[MAXN], d[MAXN];
int N, n;
void solve()
{
int x,i;
int front = 0, 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]);
}
void input()
{
while(scanf("%d",&n) == 1)
{
while(n --)
{
scanf("%s",b);
N = q[0] = 0;
for(int i = 0; i < 12; i ++)
q[0] = (q[0]<<1) + (b[i] == 'o' ? (N ++,1) : 0);
solve();
}
}
}
int main()
{
input();
return 0;
}



你可能感兴趣的:(AIR)