博弈论(巴什博弈,威佐夫博弈,尼姆博弈,附有模板及题目)

一、巴什博弈

描述:n个数,一次最多报m个,最后取光的人获胜
结论:当n为m+1的倍数时,后手必胜,其它情况均是先手必胜

模板题及模板:

A题 hdu 1846 Brave Game

#include <bits/stdc++.h>

using namespace std;
int t,m,n;

int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {

        cin>>n>>m;
        if(n%(m+1)==0)cout<<"second"<<endl;

        else cout<<"first"<<endl;
    }
    return 0;
}

二、威佐夫博弈

描述:两堆各若干的物品,任意取其中若干件物品,或同时取两堆中相同物品数,取完者获胜
结论:后手必胜的局有:(2,3)(4,7)…
满足t=floor(n-m)*(1+sqrt(5.0))/2.0; if(t==m)cout<<"后手必胜"<<endl;
当然,(2,3)可以,(3,2)自然也可以

模板题及模板:

hdu 1847 Good Luck in CET-4 Everybody!

#include <bits/stdc++.h>

using namespace std;
int t,m,n,a[105];

int main()
{
    ios::sync_with_stdio(false);

    while(cin>>n>>m)
    {
        if(n<m)swap(n,m);
        t=floor(n-m)*(1+sqrt(5.0))/2.0;
        if(t==m)cout<<"0"<<endl;
        else cout<<"1"<<endl;
    }
    return 0;
}

三、尼姆博弈

描述:有任意堆物品,双方取一堆中的任意件物品,最后取完者获胜
结论:把每堆物品数全部异或,最后等于0,那么后手必胜,不等于0则先手必胜(二进制太神奇了!!!异或太神奇了有木有!!!)

模板:

#include <bits/stdc++.h>

using namespace std;
int t,m,n,a[105];

int main()
{
    

    while(cin>>n)
    {
        int sum=0;
        for(int i=0;i<n;i++)cin>>a[i];
        for(int i=0;i<n;i++)
        {
            sum=sum^a[i];
        }
        if(sum==0)cout<<"Lose"<<endl;
        else cout<<"Win"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhoucheng_123/article/details/104197872