简单博弈论题目总结

BashGame I 

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N和K,问最后谁能赢得比赛。
例如N = 3,K = 2。无论A如何拿,B都可以拿到最后1颗石子。

//https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1066

#include<iostream>
#include<cstring>
using namespace std;
int N,K,T;
/*
巴什博奕(Bash Game):
必胜策略:设N=(K+1)r+s,先手者拿走s个,使后手者面对(K+1)的倍数的情况.若后手者拿走m个,则先手者再拿走K+1-m个.
*/
int main()
{
    cin.sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        cin>>N>>K;
        if (N<=K)
        {
            cout<<"A\n";
            continue;
        }
        else
        {
            N%=(K+1);
            if (N==0)
                cout<<"B\n";
            else
                cout<<"A\n";
        }
    }
    return 0;
}


BashGame II 

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次只能拿134颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 2。A只能拿1颗,所以B可以拿到最后1颗石子。

//https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1067

#include<iostream>
#include<cstring>
using namespace std;
int N,T;
int main()
{
    cin.sync_with_stdio(false);
    /*
    用SG函数打表
    int S[1000],SG[1000],F[3]={1,3,4};
    memset(SG,0,sizeof(SG));
    for (int i=1;i<1000;i++)
    {
        memset(S,0,sizeof(S));
        for (int j=0;j<3&&i>=F[j];j++)
            S[SG[i-F[j]]]=1;
        for (int j=0;j<1000;j++)
            if (!S[j])
                SG[i]=j,j=1123;
    }
    for (int i=0;i<100;i++)
        cout<<i<<" : "<<SG[i]<<endl;
    */
    cin>>T;
    while(T--)
    {
        cin>>N;
        N%=7;
        if (N==0||N==2)
            cout<<"B\n";
        else
            cout<<"A\n";
    }
    return 0;
}

BashGame III 

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次拿的数量只能是2的正整数次幂,比如(1,2,4,8,16....),拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 3。A只能拿1颗或2颗,所以B可以拿到最后1颗石子。(输入的N可能为大数)

//https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1068

#include<iostream>
#include<cstring>
using namespace std;
int T;
char N[1005];
int main()
{
    cin.sync_with_stdio(false);
    /*
    用SG函数打表
    int S[1000],SG[1000],F[8]={1,2,4,8,16,32,64,128};
    memset(SG,0,sizeof(SG));
    for (int i=1;i<1000;i++)
    {
        memset(S,0,sizeof(S));
        for (int j=0;j<3&&i>=F[j];j++)
            S[SG[i-F[j]]]=1;
        for (int j=0;j<1000;j++)
            if (!S[j])
                SG[i]=j,j=1123;
    }
    for (int i=0;i<100;i++)
        cout<<i<<" : "<<SG[i]<<endl;
    */
    cin>>T;
    while(T--)
    {
        cin>>N;
        int sum=0,len=strlen(N);
        for (int i=0;i<len;i++)
            sum+=N[i]-'0';
        if (sum%3==0)
            cout<<"B\n";
        else
            cout<<"A\n";
    }
    return 0;
}

BashGame IV

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次拿的数量最少1个,最多不超过对手上一次拿的数量的2倍(A第1次拿时要求不能全拿走)。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 3。A只能拿1颗或2颗,所以B可以拿到最后1颗石子。

//https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1070

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=1e9+5;
int T,Cnt=1;
long long N,F[100];
/*
斐波那契博弈
http://blog.csdn.net/acm_cxlove/article/details/7835016

必败态构成Fibonacci数列
*/
int main()
{
    cin.sync_with_stdio(false);
    F[0]=F[1]=1;
    for (int i=2;i<100&&F[i-1]<MAX;i++)
        F[i]=F[i-1]+F[i-2],Cnt=i+1;
    cin>>T;
    while(T--)
    {
        cin>>N;
        if (binary_search(F,F+Cnt,N))
            cout<<"B\n";
        else
            cout<<"A\n";
    }
    return 0;
}
WythoffGame I 

有2堆石子。A B两个人轮流拿,A先拿。每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出2堆石子的数量,问最后谁能赢得比赛。
例如:2堆石子分别为3颗和5颗。那么不论A怎样拿,B都有对应的方法拿到最后1颗。

//https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072
//证明:http://baike.baidu.com/item/%E5%A8%81%E4%BD%90%E5%A4%AB%E5%8D%9A%E5%BC%88/19858256?fr=aladdin&fromtitle=%E5%A8%81%E4%BD%90%E5%A4%AB%E5%8D%9A%E5%A5%95&fromid=7139745&type=syn#4

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int T,N,M;
/*
威佐夫博弈:
两个人如果都采用正确操作,那么面对非奇异局势,先拿者必胜;反之,则后拿者取胜.
局势(N,M)(N<M)满足N==(int)((1.0+sqrt(5.0))/2.0*(M-N))时,该局势为奇异局势,后拿者必胜.
*/
int main()
{
    cin.sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        cin>>N>>M;
        if (N>M) swap(N,M);
        if (N==(int)((1.0+sqrt(5.0))/2.0*(M-N)))
            cout<<"B\n";
        else
            cout<<"A\n";
    }
    return 0;
}
NimGame

有N堆石子。A B两个人轮流拿,A先拿。每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N及每堆石子的数量,问最后谁能赢得比赛。
例如:3堆石子,每堆1颗。A拿1颗,B拿1颗,此时还剩1堆,所以A可以拿到最后1颗石子。

//https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1069

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int N,Ans,A;
/*
尼姆博奕:
(Bouton's Theorem)对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示异或(xor)运算.
*/
int main()
{
    cin.sync_with_stdio(false);
    cin>>N;
    for (int i=0;i<N;i++)
    {
        cin>>A;
        if (!i)
            Ans=A;
        else
            Ans^=A;
    }
    if (Ans)
        cout<<"A\n";
    else
        cout<<"B\n";
    return 0;
}
//http://acm.hdu.edu.cn/showproblem.php?pid=1848
//博客学习:http://blog.csdn.net/luomingjun12315/article/details/45555495

/*
SG函数:

        首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数。例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。

        对于任意状态 x , 定义 SG(x) = mex(S),其中 S 是 x 后继状态的SG函数值的集合。如 x 有三个后继状态分别为 SG(a),SG(b),SG(c),那么SG(x) = mex{SG(a),SG(b),SG(c)}。 这样 集合S 的终态必然是空集,所以SG函数的终态为 SG(x) = 0,当且仅当 x 为必败点P时。
*/


#include<iostream>
#include<cstring>
using namespace std;
const int MAX=1e3+5;
int M,N,P,F[50],SG[MAX],S[MAX];
void GetSG(int n)
{
    memset(SG,0,sizeof(SG));
    for (int i=1;i<=n;i++)
    {
        memset(S,0,sizeof(S));
        for (int j=0;F[j]<=i&&j<50;j++)
            S[SG[i-F[j]]]=1;
        for (int j=0;j<=n;j++)
            if(!S[j])
                SG[i]=j,j=n+123;
    }
}
int main()
{
    cin.sync_with_stdio(false);
    F[0]=F[1]=1;
    for (int i=2;i<50;i++)
        F[i]=F[i-1]+F[i-2];
    GetSG(MAX);
    while(cin>>M>>N>>P&&(M||N||P))
    {
        if(SG[M]^SG[N]^SG[P])
            cout<<"Fibo\n";
        else
            cout<<"Nacci\n";
    }
    return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值