HDU 4111 Alice and Bob 【博弈】

it2022-05-05  142

传送门:HDU 4111


Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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 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了,那么这堆就不存在了);2、合并两堆石子 没有操作的就输。问是哪个赢.

AC代码:

/* 思想: 如果每堆石子数都大于1,那么最后结果肯定相当于所有的堆合并成一堆后,然后再一个一个拿掉的结果。 因为如果那种情况是赢的人一定会不断合并堆来确保他是赢的。又因为所有堆的石子数都大于1,所以输的人无法阻止他这么干。 而有些堆石子数等于1的话,就不一定是所有的合并的结果了,因为输的人可以直接把等于1的堆去掉,就破坏了结构 (合并相当于2步,去掉只需要1步)。 sg[i][j]表示有i个石子数为1的堆数,其它堆合并再取完的步数为j。若值为1则先取者胜,为0为先取者输 */ #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #define ll long long #define inf 0x3f3f3f3f using namespace std; const int N=55; int sg[N][N*1000]; int n,t; int a[N]; int getsg(int one,int sum) { if(sg[one][sum]!=-1) return sg[one][sum]; if(sum==1) return sg[one][sum]=getsg(one+1,0);//步数只需要1,放入其他1 sg[one][sum]=0; if(one>=1&&!getsg(one-1,sum))//去掉一个1 { sg[one][sum]=1; } if(one>=1&&sum&&!getsg(one-1,sum+1))//将一个1合到其他数 { sg[one][sum]=1; } if(sum>=2&&!getsg(one,sum-1))//非1数减1 { sg[one][sum]=1; } if(one>=2&&((sum==0&&!getsg(one-2,sum+2))||(sum&&!getsg(one-2,sum+3))))//将两个1合并 { sg[one][sum]=1; } return sg[one][sum]; } int main() { memset(sg,-1,sizeof(sg)); int ca=0; scanf("%d",&t); while(t--) { int one=0,sum=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); if(a[i]==1) { one++;//计算1的个数 } else { sum+=a[i]+1;//计算步数 } } if(sum) sum--;//多合并一次 getsg(one,sum); if(sg[one][sum]) printf("Case #%d: Alice\n",++ca); else printf("Case #%d: Bob\n",++ca); } return 0; }

最新回复(0)