博弈论

1. Vijos 萌萌的糖果博弈 && 吃糖果游戏ios

每次能够任意拿走其中一堆糖果;若是这时候另外一堆糖果数目多于1粒,就把它任意分红两堆,不然就把剩下的一粒糖果取走并得到此次博弈的胜利。

萌萌的糖果博弈
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     string a,b;
 8     while(cin>>a)
 9     {
10         if(a[0]=='-')break;
11         cin>>b;
12         int a1= a[a.length()-1]-'0';
13         int b1= b[b.length()-1]-'0';
14         if((a1%5==2||a1%5==3)&&(b1%5==2||b1%5==3))
15             printf("SheepDaddy\n");
16         else
17             printf("MengMeng\n");
18     }
19     return 0;
20 }

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     string a,b;
 8     while(cin>>a>>b)
 9     {
10         int a1= a[a.length()-1]-'0';
11         int b1= b[b.length()-1]-'0';
12         if((a1%5==2||a1%5==3)&&(b1%5==2||b1%5==3))
13             printf("Shadow\n");
14         else
15             printf("Matrix67\n");
16     }
17     return 0;
18 }
吃糖果游戏

 2. 巴什博奕ide

  只有一堆n个物品,两我的轮流从中取物,规定每次最少取一个,最多取m个,最后取光者为胜。spa

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,m;
 6     while(cin>>n>>m)
 7       if(n%(m+1)==0)  cout<<"后手必胜"<<endl;
 8       else cout<<"先手必胜"<<endl;
 9     return 0;
10 }
待检验

3. 威佐夫博弈(Wythoff Game)(*黄金分割).net

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

POJ 1067 code

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int a,b;
 6     while(~scanf("%d%d",&a,&b))
 7     {
 8         putchar( ((a<b?a:b)==(int)(abs(a-b)*1.618033988749895)?'0':'1') );
 9         putchar('\n');
10     }
11     return 0;
12 }
恶心的AC代码

4. 尼姆博弈(Nimm Game):blog

  有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取部分或所有物品,最少取一件,取到最后一件物品的人获胜。游戏

结论就是:把每堆物品数所有异或起来,若是获得的值为0,那么先手必败,不然先手必胜。ci

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     int n,ans,temp;
 8     while(cin>>n)
 9     {
10         temp=0;
11         for(int i=0;i<n;i++)
12         {
13             cin>>ans;
14             temp^=ans;
15         }
16         if(temp==0)  cout<<"后手必胜"<<endl;
17         else cout<<"先手必胜"<<endl;
18     }
19     return 0;
20 }
View Code

5. 斐波那契博弈:get

  有一堆物品,两人轮流取物品,先手最少取一个,至多无上限,但不能把物品取完,以后每次取的物品数不能超过上一次取的物品数的二倍且至少为一件,取走最后一件物品的人获胜。

结论是:先手胜,当且仅当n不是斐波那契数(n为物品总数)

 1 #include <iostream>  
 2 #include <string.h>  
 3 #include <stdio.h>  
 4 using namespace std;  
 5 const int N = 55;    
 6 int f[N];   
 7 void Init()  
 8 {  
 9     f[0] = f[1] = 1;  
10     for(int i=2;i<N;i++)  
11         f[i] = f[i-1] + f[i-2];  
12 }    
13 int main()  
14 {  
15     Init();  
16     int n;  
17     while(cin>>n)  
18     {  
19         if(n == 0) break;  
20         bool flag = 0;  
21         for(int i=0;i<N;i++)  
22         {  
23             if(f[i] == n)  
24             {  
25                 flag = 1;  
26                 break;  
27             }  
28         }  
29         if(flag) puts("Second win");  
30         else     puts("First win");  
31     }  
32     return 0;  
33 } 
View Code

 

 

博文参考:http://blog.csdn.net/ac_gibson/article/details/41624623

相关文章
相关标签/搜索