HDU 1907 John(博弈)

题目

参考了博客:http://blog.csdn.net/akof1314/article/details/4447709

//0 1 -2
//1 1 -1
//0 2 -1
//1 2 -1
//2 2 -2
//0 3 -1
//1 3 -1
//2 3 -1
//3 3 -2
//0 4 -1
//1 4 -1
//2 4 -1
//3 4 -1
//4 4 -2
//0 5 -1
//1 5 -1
//2 5 -1
//3 5 -1

/*
  尼姆博弈。对于N堆的糖,一种情况下是每堆都是1,那么谁输谁赢看堆数就知道;对于不都是1的话,若这些堆是奇异局势,或说他们是非奇异局势,但非奇异局势皆可以转换到奇异局势(具体转变看上一篇介绍)。
       经典的尼姆问题是谁哪拿到最后一个则谁赢,本题是拿最后一个的输。下面分析第二种情况:
1.初始给的是奇异局势的话,则先取者为输。
2.初始给的是非奇异局势的话,则先取者为赢。辗转转换非奇异、奇异的次数是相对的。
*/
#include<stdio.h>
#include<string.h>

int main(){
    int t;
    scanf("%d",&t);
    while(t--) {
        int n;
        int a[100];
        scanf("%d",&n);
        int sum=0,k=0;
        for(int i=0;i<n;i++) {
            scanf("%d",&a[i]);
            sum^=a[i];//这样子判断奇异局势吗?用 ^ ?
            if(a[i]!=1)k=1;
        }
        if(k==0) {
            if(n%2)printf("Brother\n");
            else printf("John\n");
        }
        else {
            if(sum==0)printf("Brother\n");////这样子判断奇异局势,为0奇异
            else printf("John\n");//否则非奇异
        }
    }
    return 0;
}

时间: 2024-12-25 08:11:36

HDU 1907 John(博弈)的相关文章

hdu 1907 John

取火柴的游戏 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 可将一堆全取走,但不可不取,最后取完者为胜,求必胜的方法. 若sum=0:则先取者输,后取者胜: 若sum=!0,则先取者使其变成奇异状态,先取者胜 即谁先面临奇异状态谁输: 题目2:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 可将一堆全取走,但不可不取,最后取完者为负,求必胜的方法. 分为2种情况:1:每个堆数的火柴数量为1,若为奇数堆,先取者最后取完,则输: 若为偶数堆,后取则输 2

HDU 1907 Nim博弈变形

1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形.设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败).S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜).T0:a1

POJ 3480 &amp; HDU 1907 John(尼姆博弈变形)

题目链接: PKU:http://poj.org/problem?id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1907 Description Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to e

hdu 1907(Nim博弈)

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4407    Accepted Submission(s): 2520 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 1907 John(anti nim)

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4704    Accepted Submission(s): 2720 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

HDU 1907、2907 【博弈、anti-NIM】

NIM游戏 和 anti-NIM游戏: NIM游戏,是取走最后一个石子者胜 而anti-NIM游戏,是取走最后一个石子者败 游戏通用准则: 1. 若一个局面为必胜态,则总存在一种操作方式将当前状态转化为必败态 2. 若一个局面为必败态,则所有操作都只能转化为必胜态 NIM游戏的解法: 将当前局面的所有堆的石子数进行异或(也称为:NIM sum), 1)若NIM sum=0,则当前局面为必败态 2)若NIM sum≠0,则当前局面为必胜态 #include <cstdio> #include &

hdu 4763 stone(博弈)

F - Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4764 Description Tang and Jiang are good friends. To decide whose treat it is for dinner, they are playing a game. Specifically, Tang an