HDU 4111 Alice and Bob 记忆化搜索

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 againand 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$ \le$T$ \le$4000), indicating the number of test cases.

Each test case contains several lines.

The first line contains an integer N (1$ \le$N$ \le$50).

The next line contains N positive integers A1...AN (1$ \le$Ai$ \le$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 

3
3
1 1 2
2
3 4
3
2 3 5

Sample Output 

Case #1: Alice
Case #2: Bob
Case #3: Bob

  N个数,两人轮流选一个数,可以减1,也可以加到另外一个数上。如果减到0这个数就消失了。

  大于1的数最后肯定是要加到一个数上去的,因为关键在于1可以改变胜负状态,因为1的时候可以选择减为0或者加到另一个数上,这样相当于走了一步和没走。所以一开始必胜的那个人肯定不会让另一个人把这些不为1的数在1处操作的。所以这些数等价于一个合并数,合并的时候要加上移动过去的那一步。

  dp[a][b]表示有a个1,剩下的那个合并数的值为b。进行记忆化搜索,搜索各种可能情况,要考虑完全。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 50010
#define MAXM 55
#define MAXNODE 100010*27
#define MOD 999983
typedef long long LL;
using namespace std;
int T,N,dp[MAXM][MAXN];
int DP(int a,int b){
    int& ans=dp[a][b];
    if(ans!=-1) return ans;
    if(b==1) return ans=DP(a+1,0);  //如果合并数为1,就相当于多了个1,没有合并数
    if(a>0&&!DP(a-1,b)) return ans=1;   //把一个1减掉
    if(b>0&&!DP(a,b-1)) return ans=1;   //把合并数减一
    if(a>0&&b&&!DP(a-1,b+1)) return ans=1;  //在合并数大于0的情况下把一个1加到合并数上
    if(a>1&&(b&&!DP(a-2,b+3)||!b&&!DP(a-2,2))) return ans=1;    //把一个1加到另一个1上变成2,如果合并数存在,把2和合并数合并,因为移动算1次,所以是b+3,如果合并数不存在,2就相当于一个新的合并数
    return ans=0;
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    memset(dp,-1,sizeof(dp));
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        int one=0,sum=0,t;
        for(int i=0;i<N;i++){
            scanf("%d",&t);
            if(t==1) one++;
            else sum+=t+1;
        }
        if(sum) sum--;
        printf("Case #%d: ",++cas);
        if(DP(one,sum)) printf("Alice\n");
        else printf("Bob\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值