您的位置:首页 > 大数据 > 人工智能

UVA 10651 Pebble Solitaire

2011-11-25 23:45 393 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: