反Nim博弈

原文地址:https://blog.csdn.net/xuejye/article/details/78975900

在尼姆博奕中取完最后一颗糖的人为赢家,而取到最后一颗糖为输家的就是反尼姆博奕。这道题就反尼姆

博奕的模型。在尼姆博奕中判断必胜局面的条件是所有堆石子数目相异或不等于0 。  而在反尼姆博奕中判断必胜局

面的条件有两点,满足任意一点先手都能取胜,即必胜局面。   

第一种判别方法:  15ms

                   1:各堆石子数目异或结果不等于0,且存在有石子数目大于1的石子堆。

                   2:各堆石子数目异或结果等于0,且所有石子堆数目全部为1。

第二种判别方法:  0ms

  1、若所有堆的数量都为1, 并且1的数量为偶数先手赢。
  2、若不都为1, 那么判断res即可。
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;

int main()
{
    int T;
    cin>> T;
    while(T--)
    {
        int n, res = 0, cnt = 0;
        cin>> n;
        for(int i=0; i<n; i++)
        {
            int w;
            cin>> w;
            res ^= w;
            if(w == 1) cnt++;
        }
        if(res && cnt != n) cout<< "John" <<endl;
        else if(!res && cnt == n) cout<< "John" <<endl;
        else cout<< "Brother" <<endl;


    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9339499.html