题意:
给你n 堆石子,两种操作:
1. 任选一堆石子, 数目减一, 如果为0 删除这一堆。
2. 任选两堆石子进行合并。
最后不能操作的人输掉。
问谁能赢?
思路:
记忆话搜索博弈,第一次见到。
状态找的比较巧妙把。
把所有的石子堆数分成两类。
数量为1, 数量为2, 和数量大于等于3的奇数,和数量大于等于4的偶数。
因为大于等于3 的奇数,你取一步 他取一步 ,效果等价于3.
偶数4同理。
那么直接令dp[a][b][c][d] 表示是否必胜即可。
直接按照他的规则搜索就行勒。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
using namespace std;
const int maxn = 50 + 2;
int dp[maxn][maxn][maxn][maxn];
int vis[maxn][maxn][maxn][maxn];
int ks, T;
int dfs(int a,int b,int c,int d){
int& ans = dp[a][b][c][d];
if (vis[a][b][c][d] == 1)return ans;
vis[a][b][c][d] = 1;
ans = 0;
if (a >= 1 && !dfs(a-1,b,c,d)){ ///--a
ans = 1;
}
if (b >= 1 && !dfs(a+1, b-1, c,d)){///--b
ans = 1;
}
if (c >= 1 && !dfs(a, b+1, c-1, d)){///--c
ans = 1;
}
if (d >= 1 && !dfs(a,b,c+1,d-1)){///--d
ans = 1;
}
///====================================
///合并两堆石子:
if (a >= 2 && !dfs(a-2, b + 1, c, d)){
ans = 1;
}
if (b >= 2 && !dfs(a, b-2, c, d+1)){
ans = 1;
}
if (c >= 2 && !dfs(a,b,c-2, d+1)){
ans = 1;
}
if (d >= 2 && !dfs(a,b,c,d-2+1)){
ans = 1;
}
if (a >= 1 && b >= 1 && !dfs(a-1,b-1,c+1, d)){
ans = 1;
}
if (a >= 1 && c >= 1 && !dfs(a-1, b, c-1, d+1)){
ans = 1;
}
if (a >= 1 && d >= 1 && !dfs(a-1, b, c+1, d-1)){
ans = 1;
}
if (b >= 1 && c >= 1 && !dfs(a, b-1, c-1+1, d)){
ans = 1;
}
if (b >= 1 && d >= 1 && !dfs(a, b-1, c, d-1+1)){
ans = 1;
}
if (c >= 1 && d >= 1 && !dfs(a,b,c-1+1,d-1)){
ans = 1;
}
return ans;
}
int main(){
scanf("%d",&T);
while(T--){
int n;
int a = 0,b = 0,c = 0,d = 0;
scanf("%d",&n);
for (int i = 0; i < n; ++i){
int x;
scanf("%d",&x);
if (x == 1)++a;
else if (x == 2) ++b;
else if (x & 1) ++c;
else ++d;
}
printf("Case #%d: ", ++ks);
int ans = dfs(a,b,c,d);
if (ans == 0){
puts("Bob");
}
else puts("Alice");
}
return 0;
}
Alice and Bob
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2137 Accepted Submission(s): 803
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!
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 A 1 ....A N(1 <= A i <= 1000), represents the numbers they write down at the beginning of the game.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A 1 ....A N(1 <= A i <= 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