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

【DP】UVA 10651 Pebble Solitaire 记忆化搜索

2014-09-03 16:16 399 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 111;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
int dp[1<<14];
void print(int x)
{
int out=0;
while(x)
{
int a=x%2;
if(a) out++;
x/=2;
}
printf("%d\n",out);
}
int bfs(int x)
{
queue<int>q;
q.push(x);
while(!q.empty())
{
x=q.front();
q.pop();
for(int i=0; i<12; i++)
{
if((1 << i) & x)
{
if(i<10&&(x&(1<<(i+1)))&&!(x&(1<<(i+2))))
{
int ans=x^(1<<(i+1));
ans=ans^(1<<(i+2));
ans=ans^(1<<i);
if(!dp[ans])
{
dp[ans]=1;
q.push(ans);
}
}
if(i>2&&(x&(1<<(i-1)))&&!(x&(1<<(i-2))))
{

int ans=x^(1<<(i-1));
ans=ans^(1<<(i-2));
ans=ans^(1<<i);
if(!dp[ans])
{
dp[ans]=1;
q.push(ans);
}
}
}
}
}
print(x);
}
int main()
{
char s[13];
int t;
//  IN;
scanf("%d",&t);
while(t--)
{
cler(dp,0);
scanf("%s",s);
int in=0,x=1;
for(int i=0; i<12; i++)
{
in+=x*(s[i]=='o'?1:0);
x*=2;
}
bfs(in);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: