POJ 3480 John(SJ定理博弈)题解

题意:n堆石头,拿走最后一块的输

思路:SJ定理:先手必胜当且仅当:(1)游戏的SG函数不为0且游戏中某个单一游戏的SG函数大于1;(2)游戏的SG函数为0且游戏中没有单一游戏的SG函数大于1。

参考:【博弈】Anti,Multi,Every-SG

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 4747 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
int main(){
    int T, n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        int ans = 0, flag = 0, u;
        while(n--){
            scanf("%d", &u);
            ans ^= u;
            if(u > 1) flag = 1;
        }
        if(ans){
            if(flag) printf("John\n");
            else printf("Brother\n");
        }
        else{
            if(flag) printf("Brother\n");
            else printf("John\n");
        }
    }
    return 0;
}

 

posted @ 2018-09-21 22:31  KirinSB  阅读(183)  评论(0编辑  收藏  举报