HDU 4111 Alice and Bob 博弈论

来源:互联网 发布:淘宝2013版本下载 编辑:程序博客网 时间:2024/06/05 16:43
题目链接:HDU4111

Alice and Bob

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


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
 

Source
2011 Asia ChengDu Regional Contest 
 
题意:有几堆数,每次可以把一个堆减一,也可以合并2个堆,定义取走最后一个为胜,问哪一方会获胜
题目分析:首先对应每个堆个数都大于2的情况,双方可以不受阻挡的合并每一个堆,对于一个堆只有1的情况,这里先手可以直接取走而不是合并,这时后手就不能合并了,所以对于个数1和个数大于1的堆需要分开讨论。sg[a][b]代表1堆的个数为a,多余1堆的全合并再取走的操作总数b,也可以把b看作是一个单独的大堆,这里整合所有操作方式:
1、 在b堆里取的只剩一个了,那么和sg(a+1,b)一样
2、把b和一个a堆合并
3、a堆取走1
4、b堆取走1
5、合并2个a堆
注意第5步操作要考虑之前没有b堆的情况
遍历所有的情况跑一遍sg函数即可
////  main.cpp//  HDU4111////  Created by teddywang on 2017/7/3.//  Copyright © 2017年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int sg[60][60000];int T;int n;int getsg(int a,int b){    if(sg[a][b]!=-1) return sg[a][b];    if(b==1) return getsg(a+1,0);    sg[a][b]=0;    if(a>=1&&b>0&&!getsg(a-1,b+1))        sg[a][b]=1;    if(a>=1&&!getsg(a-1,b))        sg[a][b]=1;    if(b>1&&!getsg(a,b-1))        sg[a][b]=1;    if(a>=2&&b==0&&!getsg(a-2,b+2))        sg[a][b]=1;    if(a>=2&&b>=1&&!getsg(a-2,b+3))        sg[a][b]=1;    return sg[a][b];}int main(){    memset(sg,-1,sizeof(sg));    cin>>T;    sg[0][0]=0;    int cas=1;    while(T--)    {        cin>>n;        int one=0,sum=0;        for(int i=0;i<n;i++)        {            int a;            cin>>a;            if(a==1) one++;            else sum+=a+1;        }        printf("Case #%d: ",cas++);        if(sum) sum--;        if(getsg(one,sum)==1)            cout<<"Alice"<<endl;        else cout<<"Bob"<<endl;    }}


原创粉丝点击