HDU 4111 Alice and Bob (博弈)

Alice and Bob

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


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
 
Source
 
Recommend

题目大意:

  题目是说,现在有n堆石子,A和B两人分别轮流用最优秀的策略来完成比赛,他们可以合并任意两堆石子,也可以把某一堆的某个石子减去1个。

两个任都绝对的聪明,问最后不能进行操作的选手是谁。

解题思路:

  看到博弈题目,我也很慌,基本没有怎么A过,练习的时候也没有过,还是看别人的题解才过的,这道题不难,把握住以下规律就能A。

1.全部是1的时候,是3的倍数时输否则赢;

2.只有一堆2其他全是1的时候,1的堆数是3的倍数时输否则赢;

3.其他情况下,计算出总和+堆数-1,若为偶数,且1的堆数是偶数,则一定输;

4.不在上述情况下则赢。

代码:

# include<cstdio>
# include<iostream>

using namespace std;

int main(void)
{
    int icase = 1;
    int t; scanf("%d",&t);
    while ( t-- )
    {
        int ans = 0,one = 0, flag = 1;
        int n; scanf("%d",&n);
        for ( int i = 0;i < n;i++ )
        {
            int tmp; scanf("%d",&tmp);
            if ( tmp==1 )
                one++;
            ans += tmp;
        }
        if ( ans==one||ans==one+2 )
        {
            if ( one%3==0 )
                flag = 0;
        }
        else
        {
            ans += n;
            ans--;
            if ( (ans&1)==0&&(one&1)==0 )
                flag = 0;
        }
        printf("Case #%d: ",icase++);
        if ( flag )
            puts("Alice");
        else
            puts("Bob");
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/wikioibai/p/4758882.html