24h購物| | PChome| 登入
2013-06-24 15:58:15| 人氣767| 回應0 | 上一篇 | 下一篇

[UVA][博奕dp] 1500 - Alice and Bob

推薦 0 收藏 0 轉貼0 訂閱站台

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$ 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


就兩種操作,取走一個,或者合併兩堆。沒有辦法動作者輸。

根據經驗,先將等效盤面壓縮成每堆只有 1 個 2 個 3 個 4 個。

計算這四種可能的堆數,進行狀態轉移即可。


#include <stdio.h>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int dp[55][55][55][55] = {};
int used[55][55][55][55] = {};
int dfs(int A, int B, int C, int D) {
if(A == 0 && B == 0 && C == 0 && D == 0)
return 0;
if(used[A][B][C][D] == 2)
puts("EEEEEEEE");
if(used[A][B][C][D])
return dp[A][B][C][D];
used[A][B][C][D] = 2;
int &val = dp[A][B][C][D];
val = 0;
if(A >= 1)
val |= !dfs(A-1, B, C, D);
if(A >= 2)
val |= !dfs(A-2, B+1, C, D);
if(B >= 1)
val |= !dfs(A+1, B-1, C, D);
if(B >= 2)
val |= !dfs(A, B-2, C, D+1);
if(C >= 1)
val |= !dfs(A, B+1, C-1, D);
if(C >= 2)
val |= !dfs(A, B, C-2, D+1);
if(D >= 1)
val |= !dfs(A, B, C+1, D-1);
if(D >= 2)
val |= !dfs(A, B, C, D-1);
if(A >= 1 && B >= 1)
val |= !dfs(A-1, B-1, C+1, D);
if(A >= 1 && C >= 1)
val |= !dfs(A-1, B, C-1, D+1);
if(A >= 1 && D >= 1)
val |= !dfs(A-1, B, C+1, D-1);
if(B >= 1 && C >= 1)
val |= !dfs(A, B-1, C, D);
if(B >= 1 && D >= 1)
val |= !dfs(A, B-1, C, D);
if(C >= 1 && D >= 1)
val |= !dfs(A, B, C, D-1);
used[A][B][C][D] = 1;
return val;
}
int main() {
int testcase, cases = 0;
int n, i;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
int x;
int A = 0, B = 0, C = 0, D = 0;
for(i = 0; i < n; i++) {
scanf("%d", &x);
if(x == 1) A++;
else if(x == 2) B++;
else if(x%2 == 1) C++;
else D++;
}
printf("Case #%d: %s\n", ++cases, dfs(A, B, C, D) ? "Alice" : "Bob");
}
return 0;
}
 

台長: Morris
人氣(767) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10301 - Rings and Glue
此分類上一篇:[UVA][二分] 10567 - Helping Fill Bates

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文