[BZOJ1022][SHOI2008][博弈论][Nim游戏]小约翰的游戏

来源:互联网 发布:淘宝权冠军打野 编辑:程序博客网 时间:2024/06/10 07:46
[Problem Description]
小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到最后一粒石子的人算输。小约翰相当固执,他坚持认为先取的人有很大的优势,所以他总是先取石子,而他的哥哥就聪明多了,他从来没有在游戏中犯过错误。小约翰一怒之前请你来做他的参谋。自然,你应该先写一个程序,预测一下谁将获得游戏的胜利。
[Algorithm]
博弈论Nim游戏
[Analysis]
经典的nim游戏。稍有不同的是这里是最后一个取的输。经典的nim有一个定理,所有石子个数xor起来如果==0,说明brother赢,否则john赢。这里也是这样的(因为只要选的时候少拿一个就能强迫对手拿最后一个),但是当石子全部都是1的时候,情况恰恰相反,特判一下就好了……
[Pay Attention]
不要跟经典模型混了……
[Code]
/**************************************************************    Problem: 1022    User: gaotianyu1350    Language: C++    Result: Accepted    Time:40 ms    Memory:1272 kb****************************************************************/ #include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <iostream>using namespace std; #define MAXN 600 int main(){    int testcase, n, ans, x;    scanf("%d", &testcase);    while (testcase--)    {        scanf("%d", &n);        ans = 0;        bool oneonly = true;        for (int i = 1; i <= n; i++)        {            scanf("%d", &x);            if (x != 1) oneonly = false;            ans ^= x;        }        if (!oneonly)        {            if (ans == 0)                printf("Brother\n");            else                printf("John\n");        }        else        {            if (ans == 0)                printf("John\n");            else                printf("Brother\n");        }    }}


0 0
原创粉丝点击