zoukankan      html  css  js  c++  java
  • hdu---4994---Revenge of Nim

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=4994

    Description

    Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.  ---Wikipedia 
    Today, Nim takes revenge on you. The rule of the game has changed a little: the player must remove the objects from the current head(first) heap. Only the current head heap is empty can the player start to remove from the new head heap. As usual, the player who takes the last object wins.

    Input

    The first line contains a single integer T, indicating the number of test cases. 
    Each test case begins with an integer N, indicating the number of heaps. Then N integer Ai follows, indicating the number of each heap successively, and the player must take objects in this order, from the first to the last. 
    [Technical Specification]  1. 1 <= T <= 100  2. 1 <= N <= 1 000  3. 1 <= Ai <= 1 000 000 000

    Output

    For each test case, output “Yes” if the first player can always win, otherwise “No”.

    Sample Input

    2
    1
    2
    2
    1 1

    Sample Output

    Yes
    No


    Mean:
    给你n堆东西,两个人博弈的去拿,每次最少一个,最多是一堆,必须按顺序那,
    也就是只有把第一堆的东西拿完了才能去拿第二堆东西,谁先拿完谁胜,问先手是否能胜利。


    analyse:
    显然是博弈,既然是博弈那么我们首先要干的事就是找必胜状态(或者必败状态),
    1>对于任意一组数据(先假设里面没有某一堆里面的个数是1的时候)先手肯定是必胜的,因为先手可以每一次都只给这一堆留一个,让后手去拿这一个,到最后一堆的时候一下子全部拿走,
    2>现在把有1的情况加进去,如果在开始就遇到连续的1的话就有可能失去主动权,只有开头连续个1的个数是偶数的时候最后主动权才还是自己的,
    3>中间部分有1的情况不用考虑,我们可以先手可以通过全拿完或者给后手留一个来调节自己的必胜状态
    4>还有一个就是全是1的时候记得特判一下。
     
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cmath>
    #include<vector>
    #include<queue>
    #include<algorithm>
    
    using namespace std;
    typedef long long LL;
    
    const int maxn=500009;
    const int INF=0x3f3f3f3f;
    const int mod=2009;
    
    int main()
    {
        int T;
        scanf("%d", &T);
    
        while(T--)
        {
            int n, num;
            scanf("%d", &n);
    
            int sign=0;
            for(int i=1; i<=n; i++)
            {
                scanf("%d", &num);
                if(num!=1 && !sign)
                    sign=i;
            }
            ///!sign&&n%2判断如果每一堆都是1,n为奇数赢
            ///sign&&(sign-1)%2==0开头连续个1的个数是偶数才会赢,同时也包括没有1的情况
            if((!sign&&n%2) || (sign&&(sign-1)%2==0))
                puts("Yes");
            else
                puts("No");
        }
        return 0;
    }
  • 相关阅读:
    C# WPF 窗体传递消息
    WPF ProgressBar 样式
    WPF的TextBox以及PasswordBox显示水印文字
    Win8.1 Hyper-V 共享本机IP上网
    ASP.NET MVC入门-Startup 类:注册服务和使用中间件
    ASP.NET MVC入门-Program类:程序的入口
    Unity3d AssetBundle 资源加载与管理
    C#考核知识点总结
    数据结构与算法之美-字符串匹配(上)
    LeetCode-探索链表-综合问题
  • 原文地址:https://www.cnblogs.com/w-y-1/p/5796364.html
Copyright © 2011-2022 走看看