【板子】博弈论

这里存放三种基础博弈论的板子。

看代码趴,注释写了应用情况。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 int n,m,x;
 7 
 8 //巴什博弈 
 9 //只有一堆n个物品,两个人轮流从这堆物品中取物,
10 //规定每次至少取一个,最多取m个。最后取光者得胜。
11 void bash(){
12     cin>>n>>m;
13     if(n%(m+1)){
14         cout<<"Win"<<endl;
15     }
16     else{
17         cout<<"Lose"<<endl;
18     }
19 }
20 
21 //威佐夫博弈
22 //有两堆各若干个物品,两个人轮流从某一堆或同时从两堆中取同样多的物品,
23 //规定每次至少取一个,多者不限,最后取光者得胜 。
24 void Wythoff(){
25     double k = (1+sqrt(5.0))/2;
26     cin>>n>>m;
27     if(n > m){
28         swap(n,m);
29     }
30     int t = m - n;
31     if( n == int(1.0 * t * k)){
32         cout<<"Lose"<<endl;
33     }
34     else{
35         cout<<"Win"<<endl;
36     }
37 }
38 
39 //尼姆博弈
40 //有n堆石子,从任意一堆石子中取出任意数量的石子
41 //规定至少取一颗,至多取出这一堆剩下的所有石子,最后取光者得胜。
42 
43 void nim(){
44     cin>>n;
45     int ans = 0;
46     for(int i = 1; i<= n; i++){
47         cin>>x;
48         if(i == 1){
49             ans = x; 
50         }
51         ans ^= x;
52     }
53     if(ans == 0){
54         cout<<"Lose"<<endl;
55     }
56     else{
57         cout<<"Win"<<endl;
58     }
59 }
60 
61 int main(){
62 
63     return 0;
64 }

猜你喜欢

转载自www.cnblogs.com/Asumi/p/9758972.html