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

#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;
}

posted @ 2014-09-03 16:16  kewowlo  阅读(130)  评论(0编辑  收藏  举报