HDU 4994-Revenge of Nim(博弈论)

来源:互联网 发布:淘宝偷图技巧 编辑:程序博客网 时间:2024/05/18 02:31

Revenge of Nim
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4994
Appoint description: 

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

21221 1
 

Sample Output

YesNo

题意:有 n 个 heap(假设从左至右编号为1~n),每个 heap 上有一些 objects。有两个player,轮流从左至右的 heap 上取走 object(1 <= 取走数 <= 当前heap上的最多objects数),规定当前的 heapi 如果还有object,那么不能取走heapi+1的object,直到把 heapi 的 object 全部取光。

思路:在这个题中,如果存在大于1的堆的话,谁控制了第一个大于一的堆谁赢,如果没有大于一的堆,那就看n的奇偶性从而确定最后一个是谁。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;int a[10010];int main(){    int T,n,i,j;    int cnt_1;//在第一个大于1的堆出现之前连续1的个数    int cnt_n;//看是否有大于1的堆    scanf("%d",&T);    while(T--) {        scanf("%d",&n);        cnt_1=cnt_n=0;        for(i=0; i<n; i++)            scanf("%d",&a[i]);        for(i=0; i<n; i++) {            if(a[i]==1)                cnt_1++;            else {                cnt_n=1;                break;            }        }        if(cnt_n==1) {            if(cnt_1%2)                printf("No\n");            else                printf("Yes\n");        } else {            if(n%2)                printf("Yes\n");            else                printf("No\n");        }    }    return 0;}


0 0