Ultraviolet-1500 Alice and Bob (DP + game)

Source: Internet
Author: User

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 successfully Ted a new game, as they usually did.

The rule of the new game is quite simple. At the beginning of the game, they write downNRandom 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 integerT(1T4000), indicating the number of test cases.

Each test case contains several lines.

The first line contains an integerN(1N50 ).

The next line containsNPositive IntegersA1...AN (1AI1000), 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', WhereXIs the test case number (starting with 1) andYIs either"Alice"Or"Bob".

Sample Input
331 1 223 432 3 5
Sample output
Case #1: alicecase #2: bobcase #3: Bob has n numbers. You can subtract one or two numbers, and then remember their sum, when two people take turns to operate, they cannot calculate the loss. Alice first hands and asks the result. thought: if the number of stones in each heap is greater than 1, the final result must be equivalent to merging all the heaps into a heap, then the results are removed one by one. If that is the case, the winner will always merge the heap to make sure that he wins. Because the number of stones in all heaps is greater than 1, the loser cannot stop him from doing so. If the number of heap stones is equal to 1, it is not necessarily the result of all the merge operations, because the loser can directly remove the heap equal to 1, the structure is destroyed (merging is equivalent to two steps, and removing only one step is required ). DP [I] [J] indicates the number of heaps with 1 stones, and the number of completed steps after merging other heaps is J. If the value is 1, the first winner wins. If the value is 0, the first winner loses. There are several situations: taking a heap away with only 1; merging two heap with 1; and subtracting one from not 1; add a heap of 1 to a heap of not 1.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;int dp[60][60000];int dfs(int i, int j) {if (dp[i][j] != -1)return dp[i][j];if (j == 1)return dp[i][j] = dfs(i+1, 0);dp[i][j] = 0;if (i >= 1 && !dfs(i-1, j))dp[i][j] = 1;else if (j >= 1 && !dfs(i, j-1))dp[i][j] = 1;else if (i >= 1 && j > 0 && !dfs(i-1, j+1))dp[i][j] = 1;else if (i >= 2 && ((j >= 1 && !dfs(i-2, j+3)) || (j == 0 && !dfs(i-2, 2))))dp[i][j] = 1;return dp[i][j];}int main() {int t, n, tmp, cas = 1;scanf("%d", &t);memset(dp, -1, sizeof(dp));dp[0][0] = 0;while (t--) {int a = 0, b = 0;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &tmp);if (tmp == 1)a++;else b += tmp + 1;}if (b)b--;printf("Case #%d: ", cas++);if (dfs(a, b))printf("Alice\n");else printf("Bob\n");}return 0;}


Ultraviolet-1500 Alice and Bob (DP + game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.