博弈论-威佐夫博弈

威佐夫博弈(Wythoff Game):

有两堆各若干的物品,两人轮流从其中一堆取至少一件物品,至多不限,或从两堆中同时取相同件物品,规定最后取完者胜利。

直接说结论了,若两堆物品的初始值为(x,y),且x<y,则另z=y-x;

记w=(int)[((sqrt(5)+1)/2)*z  ];

若w=x,则先手必败,否则先手必胜。
 

代码:

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
    int n1,n2,temp;
    while(cin>>n1>>n2)
    {
        if(n1>n2)  swap(n1,n2);
        temp=floor((n2-n1)*(1+sqrt(5.0))/2.0);
        if(temp==n1) cout<<"后手必胜"<<endl;
        else cout<<"先手必胜"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39507723/article/details/83443155