hdu 4994(博弈)

Revenge of Nim

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 562    Accepted Submission(s): 290


Problem 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
 

 

Source
 
题意:按照顺序取完n堆石子,每次取一个或者一堆。
关键点:看出取到第一个>1的石子堆的人是必胜态。
/**
对于某个人来说,只要他能够取到第一个>1的堆,那么他就可以决定后面的人的状态了,所以第一个取到>1的堆的人
是赢家。所以我们只要讨论前面有多少==1的堆就行了。
2.如果里面存在>1的堆,那么在第一个>1的堆前面有偶数个=1的堆先手才会赢
1.如果没有>1的堆,那么奇数个=1的堆先手就会赢。
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;

int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        int n,cnt=0,v;
        bool flag = false;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&v);
            if(v>1&&!flag)
            {
                flag = true;
                cnt = i-1;
            }
        }
        if(!flag) cnt = n;
        if(flag){
            if(cnt%2==1) printf("No\n");
            else printf("Yes\n");
        }else{
            if(cnt%2==0) printf("No\n");
            else printf("Yes\n");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5642593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值