hdu 4111 Alice and Bob 博弈论 sg函数

来源:互联网 发布:95 知乎 编辑:程序博客网 时间:2024/05/21 07:56

Problem Description
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new game, as they usually did.
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either:
1. Decrease a number by one.
2. Erase any two numbers and write down their sum.
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!
 

Input
The first line contains an integer T(1 <= T <= 4000), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A1 ....AN(1 <= Ai <= 1000), represents the numbers they write down at the beginning of the game.
 

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is either "Alice" or "Bob".
 

Sample Input
331 1 223 432 3 5
 

Sample Output
Case #1: AliceCase #2: BobCase #3: Bob


题意:

又n堆数,两个人轮流取,能进行的操作:1. 使一堆数减一    2.合并两堆数 


分析:

考虑如果每个数都大于1,必胜方为了保证操作总数是不变的,会一直合并,保证必败方没有机会对数为1的情况进行操作。所以操作总数=数的和+堆数-1

而如果存在数为1的情况,分情况讨论,使用sg函数来判断。具体看代码:

#include <iostream>#include<bits/stdc++.h>using namespace std;int sg[60][66000];int getsg(int x,int y){    if(sg[x][y]!=-1)    return sg[x][y];    if(y==1)    return sg[x][y]=getsg(x+1,0);    sg[x][y]=0;    if(y>0&&!getsg(x,y-1))  sg[x][y]=1;    if(x>0&&!getsg(x-1,y))    sg[x][y]=1;    if(x>1&&y==0&&!getsg(x-2,y+2))  sg[x][y]=1;    if(x>1&&y>0&&!getsg(x-2,y+3))   sg[x][y]=1;    if(x>0&&y>0&&!getsg(x-1,y+1))   sg[x][y]=1;    return sg[x][y];}int main(){    int T,kase=0,n;    memset(sg,-1,sizeof(sg));    cin>>T;    while(T--)    {        scanf("%d",&n);        int sum=0,one=0,t;        for(int i=0;i<n;i++)        {            scanf("%d",&t);            if(t==1)    one++;            else    sum+=t+1;        }        if(sum) sum--;        if(getsg(one,sum))  printf("Case #%d: Alice\n",++kase);        else    printf("Case #%d: Bob\n",++kase);    }}