博弈论

巴什博弈:

Tang and Jiang are good friends. To decide whose treat it is for dinner, they are playing a game. Specifically, Tang and Jiang will alternatively write numbers (integers) on a white board. Tang writes first, then Jiang, then again Tang, etc... Moreover, assuming that the number written in the previous round is X, the next person who plays should write a number Y such that 1 <= Y - X <= k. The person who writes a number no smaller than N first will lose the game. Note that in the first round, Tang can write a number only within range [1, k] (both inclusive). You can assume that Tang and Jiang will always be playing optimally, as they are both very smart students.

 

InputThere are multiple test cases. For each test case, there will be one line of input having two integers N (0 < N <= 10^8) and k (0 < k <= 100). Input terminates when both N and k are zero.
OutputFor each case, print the winner's name in a single line.
Sample Input

1 1
30 3
10 2
0 0

Sample Output

Jiang
Tang
Jiang

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long int
using namespace std;

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0&&m==0)
            break;
        if( !( (n-1)%(m+1) ) )
            printf("Jiang\n");
        else
            printf("Tang\n");
    }
    return 0;
}

摘自于:https://blog.csdn.net/u011613321/article/details/12142861

威佐夫博弈:

有两堆石子,数量任意,可以不同。游戏开始由两个人轮流取石子。游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子;二是可以在两堆中同时取走相同数量的石子。最后把石子全部取完者为胜者。现在给出初始的两堆石子的数目,如果轮到你先取,假设双方都采取最好的策略,问最后你是胜者还是败者。

Input输入包含若干行,表示若干种石子的初始情况,其中每一行包含两个非负整数a和b,表示两堆石子的数目,a和b都不大于1,000,000,000。Output输出对应也有若干行,每行包含一个数字1或0,如果最后你是胜者,则为1,反之,则为0。Sample Input

2 1
8 4
4 7

Sample Output

0
1
0

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
double p=(sqrt((double)5)+1)/double(2);
int main (){
    int a,b,c;
        while(scanf("%d%d",&a,&b)!=EOF){
        c=abs(a-b);
        a=a>b?b:a;
        if(a==(int)(p*c)) printf("0\n");
        else printf("1\n");
    }
    return 0;
}

摘自于:https://blog.csdn.net/dgq8211/article/details/7397876

菲波那切博弈:

1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍。取完者胜.先取者负输出"Second win".先取者胜输出"First win".

Input输入有多组.每组第1行是2<=n<2^31. n=0退出.
Output先取者负输出"Second win". 先取者胜输出"First win".
参看Sample Output.
Sample Input

2
13
10000
0
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <climits>
using namespace std;

int a,b;
int main()
{
    int n,ts;
    bool mark;
    while(scanf("%d",&n) && n){
        a = 2,b = 3;
        mark = false;
        while(a<=n){
            if(a==n || b==n){
                mark = true;
                break;
            }
            ts = (a  + b);
            a = b;
            b = ts;
        }
        if(mark){
            printf("Second win\n");
        }else{
            printf("First win\n");
        }
    }
    return 0;
}

加深理解请戳这里 >> https://www.cnblogs.com/jiu0821/p/4638165.html <<

猜你喜欢

转载自www.cnblogs.com/coder-tcm/p/8905868.html