1 .巴什博弈
只有一堆物品,两人轮流从中取物,规定每次最少取 1个 最多取m个,最后取完者胜
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>n)
if(n%(m+1)==0 cout<<"后手必胜"<<endl;
else cout<<"先手必胜"<<endl;
return 0;
例hdu2897http://acm.hdu.edu.cn/showproblem.php?pid=2897
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
int n, p, q;
while(~scanf("%d %d %d", &n, &p, &q)) {
if(n % (p + q) == 0) puts("WIN");
else if(n % (p + q) <= p) puts("LOST");
else puts("WIN");
}
return 0;
}
2.NIM博弈
有任意堆物品,每堆个数任意,双方轮流取物品,每次只能从一堆物品中取走部分或者全部,至少取1个,取到最后一件物品的人获胜。
结论:把每堆物品全部异或起来如果为0则先手必败否则先手必胜
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int n,ans,temp;
while(cin>>n)
{
temp=0;
for(int i=0;i<n;i++)
{
cin>>ans;
temp^=ans;
}
if(temp==0) cout<<"后手必胜"<<endl;
else cout<<"先手必胜"<<endl;
}
return 0;
}
3.斐波那契博弈
有一堆个数为n的石子,满足:
1)先手不能第一次把石子全取完;
2)之后每次可以取石子介于1-2倍对手取走石子数之间。
最后一个取走石子胜。
结论:当石子个数是斐波那契数的时候 先手必败;
#include <stdio.h>
typedef long long ll;
ll fib[50];
int main (){
int n;
fib[0]=1;
fib[1]=2;
for (int i=2;i<=50;i++)
fib[i]=fib[i-1]+fib[i-2];
while (scanf("%d",&n)!=EOF){
int flag=1;
for (int i=0;i<=50&&flag;i++)
if (fib[i]==n)
flag=0;
if (flag)
printf ("first is winner\n");
else
printf ("second is winner\n");
}
return 0;
}
4.威佐夫博弈
有两堆物品 每堆有若干个 两人轮流取物品 取法:
1)从一堆中至少取一个;
2)从两堆中取相同个数
判断 x,y是否为奇异点(必败点)
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <cmath>>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{ int x,y;
cin>>x>>y;
double i=min(x,y);
double j=max(x,y);
if(i==(int)((j-i)*(sqrt(5)+1)/2) )cout<<"B"<<endl;
else cout<<"A"<<endl;
}
return 0;
}