hdu4994——Revenge of Nim(博弈)

来源:互联网 发布:美洲狮步战车数据性能 编辑:程序博客网 时间:2024/04/29 17:05

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

找必胜点和必败点,从后往前找,最后一个一定是必胜点,所以要求前一个一定要是另一个人拿到最后一堆,如果这一个只有一个,那还要根据前面的来判断,但如果是大于一个,那只要第一个人有机会拿这堆,这能拿到只剩下一个给另一个人,所以还要预先处理所有堆

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <math.h>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define MAXN 100005#define Mod 1000000007using namespace std;long long num[10010];int deal[10010];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=0; i<n; ++i)            scanf("%I64d",&num[i]);        if(num[0]==1)            deal[0]=1;        if(num[0]>1)            deal[0]=3;        for(int i=1;i<n;++i)        {            if(num[i-1]==1)            {                if(deal[i-1]==1)                    deal[i]=2;                else if(deal[i-1]==2)                    deal[i]=1;                else                    deal[i]=3;            }            else                deal[i]=3;        }        int pre=1,flag=0;        for(int i=n-2; i>0; --i)        {            if(pre==1)                pre=2;            else                pre=1;            if(num[i]>1)            {                if(pre==1&&deal[i]==2)                {                    flag=1;                    break;                }                else if(pre==1&&deal[i]==1)                    pre=1;                else if(pre==1&&deal[i]==3)                    pre=2;                else if(pre==2)                    pre=1;            }        }        if(flag)            printf("No\n");        else        {            if(pre==2)                printf("Yes\n");            else            {                if(num[0]>1)                    printf("Yes\n");                else                    printf("No\n");            }        }    }    return 0;}
0 0