博弈随笔

威佐夫博弈(参考博客):

例题:https://www.nowcoder.com/acm/contest/116/G

根据规律可发现必败态(奇异局势):(0,0)、(1,2)、(3,5)、(4,7)、(6,10)、(8,13)、(9,15)、(11,18)、(12,20)....

套模板:

 1 //威佐夫博弈模板
 2 #include <stdio.h>
 3 #include <math.h>
 4 const double Gsr=(1+sqrt(5.0))/2;
 5 void swap(int &a,int &b)
 6 {
 7     int t=b;  b=a;  a=t;
 8 }
 9 int main()
10 {
11     int a,b;
12     while(~scanf("%d%d",&a,&b))
13     {
14         if(a>b)
15             swap(a,b);
16         if(a == (int)(Gsr*(b-a))) //奇异局势,先拿者输
17             puts("Lao Wang");
18         else
19             puts("Xiao Ren");
20     }
21     return 0;
22 }

巴什博奕(参考博客):两个人先后报数,最少报1最多报m个数,先报到n的获胜。

模板:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,m;
 6     while(cin>>n>>m)
 7     {
 8         if(n%(m+1)==0)
 9             cout<<"houshoubisheng"<<endl;
10         else cout<<"xianshoubisheng"<<endl;
11     }
12     return 0;
13 }

猜你喜欢

转载自www.cnblogs.com/zxhyxiao/p/8981292.html