codeforces63E Sweets Game DP,dfs,博弈

来源:互联网 发布:淘宝已买到宝贝提取器 编辑:程序博客网 时间:2024/06/08 08:17

点击打开链接

一道很好的题目,看似是一道博弈,竟然可以用dp来做,dp[s]表示这一状态能否获胜,如果存在走一步后得到状态ss,ss状态必然失败,那么s状态必然获胜,否则必然失败,状态转移先用数组处理一下就好写了。

#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>#include<map>#include<algorithm>#include<queue>#include<set>#define inf 1000000000#define pi acos(-1.0)#define eps 1e-8#define seed 131using namespace std;typedef pair<int,int> pii;typedef unsigned long long ull;typedef long long ll;const int maxn=100005;int dp[1<<20];char ch;int sz[15]={3,4,5,4,3,3,4,5,4,3,3,4,5,4,3};int d[15][5]={    {0,1,2},    {3,4,5,6},    {7,8,9,10,11},    {12,13,14,15},    {16,17,18},    {0,3,7},    {1,4,8,12},    {2,5,9,13,16},    {6,10,14,17},    {11,15,18},    {7,12,16},    {3,8,13,17},    {0,4,9,14,18},    {1,5,10,15},    {2,6,11},};int dfs(int m){    if(dp[m]!=-1)        return dp[m];    for(int i=0;i<15;i++)    {        for(int j=0;j<sz[i];j++)        {            for(int k=j;k<sz[i];k++)            {                bool flag=true;                int pm=m;                for(int f=j;f<=k;f++)                {                    if(pm&(1<<d[i][f]))                        pm-=(1<<d[i][f]);                    else                    {                        flag=false;                        break;                    }                }                if(flag)                {                    if(dfs(pm)==0)                        return dp[m]=1;                }            }        }    }    return dp[m]=0;}int main(){    int mark=0;    for(int i=0;i<19;i++)    {        cin>>ch;        //cout<<ch<<endl;        if(ch=='O')        {            mark|=(1<<i);            //cout<<i<<endl;        }    }    memset(dp,-1,sizeof(dp));    if(dfs(mark))        printf("Karlsson\n");    else        printf("Lillebror\n");    return 0;}


0 0