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

UVA 10651 Pebble Solitaire

2016-04-15 19:32 441 查看
【题意】12个位置,有些有鹅卵石,有些是空的,2个连续的鹅卵石,如果其左边连着的那个是空的,那么第二个鹅卵石可移动到那个空的位置上,并移除第1个鹅卵石;如果其右边连着的那个是空的,那么第一个鹅卵石可移动到那个空的位置上,并移除第2个鹅卵石。问最后最少可以剩下多少个鹅卵石?

【分析】由于3个连续出现的鹅卵石才有可能发生交换,所以枚举所有能够出现的状态,交换直接相邻3位取异或即可!记得回溯!

【AC代码】

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int inf = 0x3f3f3f3f;
int ans,n;
char s[13];
int dfs(int sta)
{
int temp,last=0;
for(int i=1; i<=10; i++)
{
if((sta&(1<<(i-1))&&sta&(1<<i)&&!(sta&(1<<(i+1))))||(!(sta&(1<<(i-1)))&&sta&(1<<i)&&sta&(1<<(i+1))))
{
sta^=(1<<(i-1));
sta^=(1<<i);
sta^=(1<<(i+1));
temp = dfs(sta);
ans = min(ans,temp);
sta^=(1<<(i-1));
sta^=(1<<i);
sta^=(1<<(i+1));
}
}
if(last==0)
{
int cnt=0;
for(int i=0; i<12; i++)
{
if(sta&(1<<i))cnt++;
}
ans = min(ans,cnt);
}
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
{
int sta=0;
scanf("%s",s);
for(int j=0; j<12; j++)
{
if(s[j]=='o')
{
sta = sta^(1<<j);
}
}
ans = inf;
dfs(sta);
printf("%d\n",ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: