[nim博弈扩展 sg函数] UVALive 3668 A Funny Stone Game

题目链接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=242&page=show_problem&problem=1669

Root :: Regionals 2006 :: Asia - Beijing Submit Download as PDF Problem Stats

Regionals 2006 >> Asia - Beijing

3668 - A Funny Stone Game

Time limit: 3.000 seconds

题目意思:

有n堆石头,每次可以选择三堆i,j,k.要求(i<j<=k) 且第i堆至少为1,从第i堆拿一个石头给第j和第k堆。两个人轮流玩游戏,最后谁不能操作就输。问先者能否会赢,如果会赢,则第一次应该怎样选择,如果有多个选择,输出字典序最小的。

解题思路:

nim博弈的扩展

先只考虑一个石子的转移情况,位置i的一个石子,它的后继状态是位置j和k的一个石子,相当于子游戏,而子游戏的抑或值mex(sg[j]^sg[k])为当前状态的sg值。

求出每个位置有一个石子的sg值后。判断所有石子sg值抑或是否为0,为0则表示必败状态。否则把O(n^3)暴力尝试移动哪几堆,把sg[i]转移到sg[j]^sg[k] ,计算转移后的状态是否为必败态。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 33

int sg[Maxn],sa[Maxn],n;
bool mex[111000];
int N;

void init()
{
    N=23;
    for(int i=N;i>=1;i--)
    {
        memset(mex,false,sizeof(mex));
        for(int j=i+1;j<=N;j++)
            for(int k=j;k<=N;k++)
                mex[sg[j]^sg[k]]=true;
        for(int j=0;;j++)
            if(!mex[j])
            {
                sg[i]=j;
                break;
            }
    }
}
void solve()
{
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=ans^(sg[N-n+i]*(sa[i]&1)); //奇数次数算一次,偶数次抑或后为0
    //printf(":%d\n",ans);
    //system("pause");

    if(!ans)
    {
        printf("-1 -1 -1\n");
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(!sa[i])
            continue;
        for(int j=i+1;j<=n;j++)
        {
            for(int k=j;k<=n;k++)
            {
                if(!(sg[N-n+i]^sg[N-n+j]^sg[N-n+k]^ans))
                {
                    printf("%d %d %d\n",i-1,j-1,k-1);
                    //system("pause");
                    return ;
                }
            }
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   init();
   int cas=0;
   while(~scanf("%d",&n)&&n)
   {
       for(int i=1;i<=n;i++)
            scanf("%d",&sa[i]);
       printf("Game %d: ",++cas);
       solve();
   }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/cc_again/article/details/40144613