uvalive 5760 Alice and Bob (组合游戏,dp)

题目链接: http://vjudge.net/problem/viewProblem.action?id=25636

对于>1的堆,必然会被其中一人全部合并。

然后就是二维dp,dp[非1堆的操作数][1堆个数]。

 1 #include <stdio.h>

 2 #include <cstring>

 3 #include <cstdlib>

 4 #include <algorithm>

 5 #include <cmath>

 6            

 7 using namespace std;

 8 #define lson o<<1

 9 #define rson o<<1|1

10 #define max(a,b) (a)>(b)?(a):(b)

11 #define min(a,b) (a)<(b)?(a):(b)

12 #define INF 200000000

13            

14 typedef long long ll;

15 int dp[60][50100];

16 int dfs(int one,int tot){

17     if(dp[one][tot]>-1)return dp[one][tot];

18     if(tot==1)return dp[one][tot]=dfs(one+1,0);

19     if(one==0)return dp[one][tot]=tot&1;

20     if(tot>0&&!dfs(one,tot-1))return dp[one][tot]=1;

21     if(!dfs(one-1,tot))return dp[one][tot]=1;

22     if(tot&&!dfs(one-1,tot+1))return dp[one][tot]=1;

23     if(one>=2)

24        if((tot&&!dfs(one-2,tot+3))||(!tot&&!dfs(one-2,tot+2)))

25            return dp[one][tot]=1;

26     

27     return dp[one][tot]=0;

28 }           

29 int main(){

30    int t,cs,n;

31    scanf("%d",&t);

32    memset(dp,-1,sizeof dp);

33    dp[0][0]=0;

34    for(cs=1;cs<=t;cs++){

35       scanf("%d",&n);

36       int x,one=0,tot=0;

37       for(int i=0;i<n;i++){

38          scanf("%d",&x);

39          if(x==1)one++;

40          else

41             tot+=x+1;

42       }

43       if(tot)tot--;

44       printf("Case #%d: ",cs);

45       if(dfs(one,tot))printf("Alice\n");

46       else printf("Bob\n");

47    }

48    return 0;

49 }

 



 

你可能感兴趣的:(live)