zoukankan      html  css  js  c++  java
  • HDU 4994 博弈。

    F - 6
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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




    思路: 博弈, 先找必胜状态或必败状态, 判断控制权在谁手里,他就必胜, 判断控制权在谁手里就判断第一个非1的数钱有几个1 如果是偶数个控制权就在第一个手里。


    代码:

    #include<stdio.h>
    #include<string.h>
    
    #define N 110000
    int main(void)
    {
    int t, i, j, n, f, ok;
    int a[N];
    scanf("%d", &t);
    
    while(t--)
    {
    f = 0;
    ok = 1;
    
    scanf("%d", &n);
    
    for(i = 0; i < n; i++)
    scanf("%d", &a[i]);
    
    for(i = 0; i < n; i++)
    {
    if(a[i] > 1)
    {
    ok = 0;//判断是否全是1.
    break;
    }
    
    else
    f++;//判断第一个不是1的位置。
    }
    
    if(ok==0)//如果有非1的存在,就判断控制权再谁手里, 如果在第一非1的数前有偶数个1控制权在第一个人手里。
    {
    if(f%2)
    printf("No
    ");
    else
    printf("Yes
    ");
    }
    else//如果全是1的话,如果有奇数堆第一个人必胜。
    {
    if(f%2)
    printf("Yes
    ");
    else
    printf("No
    ");
    }
    
    }
    return 0;
    
    }
  • 相关阅读:
    一份详尽的 Java 问题排查工具清单,值得收藏!
    专业解决 MySQL 查询速度慢与性能差!
    马士兵对话京东T6阿里P7(薪水):月薪5万,他为何要离职?
    Java中的注解到底是如何工作的?
    一道简单的面试题,难倒各大 Java 高手!
    疯狂618,当当买书打 3 折!
    SQL才是世界上最牛逼的语言!
    程序员必须掌握的职场黑话,你知道几个?
    面试问我 Java 逃逸分析,瞬间被秒杀了。。
    redis 介绍与操作
  • 原文地址:https://www.cnblogs.com/dll6/p/5782820.html
Copyright © 2011-2022 走看看