hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4111
Description
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
Sample Output
Case #1: Alice
Case #2: Bob
Case #3: Bob
HINT
题意
给你n堆石头,你有两个选择,1.减少一堆石头数量1,2.合并两堆石头
如果遇到不能操作的情况就算输
题解:
这道题当成dp来做,dp[i][j]表示现在有i堆只有1个,j表示现在我能够操作次数
操作次数等于 sigma(a[i])-n+1,其中a[i]>1,n为满足条件的a[i]的个数
------------------------
那么转移方程就出来了,1个的,要么消去,要么和1个的合并,要么和多个的合并
然后就可以记忆化搜索搞了
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50051
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int dp[][maxn];
int a[maxn]; int dfs(int a,int b)//a 表示还有多少组为1 b表示还剩下多少次操作次数
{
if(dp[a][b]!=-)
return dp[a][b];
dp[a][b]=;//其他情况都是不可操作的
if(b==)
return dp[a][b]=dfs(a+,);//只剩一个单独的一,加入其它1中
if(a>=&&!dfs(a-,b))//直接去掉一个1
dp[a][b]=;
if(a>=&&b&&!dfs(a-,b+))//将一个1合到其它数中
dp[a][b]=;
if(a>=&&((b&&!dfs(a-,b+))||(b==&&!dfs(a-,))))//将2个1并起来
dp[a][b]=;
if(b>=&&!dfs(a,b-))//减小一
dp[a][b]=; return dp[a][b];
} int main()
{
//freopen("test.txt","r",stdin);
memset(dp,-,sizeof(dp));
int t=read();
for(int cas=;cas<=t;cas++)
{
int flag=;
int sum=;
int n=read();
for(int i=;i<n;i++)
{
a[i]=read();
if(a[i]==)
flag++;
else
sum+=a[i]+;
}
sum--;
dfs(flag,sum);
if(dp[flag][sum])
printf("Case #%d: Alice\n",cas);
else
printf("Case #%d: Bob\n",cas);
}
}
hdu 4111 Alice and Bob 记忆化搜索 博弈论的更多相关文章
- hdu 4111 Alice and Bob(中档博弈题)
copy VS study 1.每堆部是1的时候,是3的倍数时输否则赢: 2.只有一堆2其他全是1的时候,1的堆数是3的倍数时输否则赢: 3.其他情况下,计算出总和+堆数-1,若为偶数,且1的堆数是偶 ...
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
- hdu 1142(迪杰斯特拉+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 1176免费馅饼(记忆化搜索)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 2476 String painter(记忆化搜索, DP)
题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...
- hdu 5389 Zero Escape(记忆化搜索)
Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi ...
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
随机推荐
- Axure RP 授权码
Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...
- 二. Jmeter--关联
1. 首先建立一个线程组(Thread Group),为什么所有的请求都要加入线程组这个组件呢?不加不行吗?答案当然是不行的.因为jmeter的所有任务都必须由线程处理,所有任务都必须在线程组下面创建 ...
- php审计学习:xdcms2.0.8注入
注入点Fields: 注册页面会引用如下方法: $fields 变量是从 $fields=$_POST['fields']; 这里获取, 在代码里没有过滤. 打印 fields 数据查看: 从代码上看 ...
- 码源中国.gitignore忽略文件配置
码源中国.gitignore忽略文件配置 ## Ignore Visual Studio temporary files, build results, and ## files generated ...
- 142.Linked List Cycle II---双指针
题目链接 题目大意:141题目的扩展,给出单链表,判断是否有环,如果有环,找出环的开始的结点,如果没有环,返回null. 法一(借鉴):在已经找出单链表环的基础上再找开始结点,要时刻记住这个环不一定是 ...
- Python下urllib2应用
#coding=utf-8 import urllib,urllib2 url = 'http://www.xxx.com' values = {'wd' : 'python', 'language' ...
- Qt跨线程调用错误解析及解决办法
错误提示:Error: Cannot create children for a parent that is in a different thread. 错误案例分析 新建SerialLink子线 ...
- IP负载均衡技术
参考链接:http://www.360doc.com/content/12/1117/19/820209_248442094.shtml
- UUID工具类
在数据库中,一张数据表中的主键,通常会用自增的整型值或者UUID字符串来表示.其中,UUID可以产生全球唯一的标识符,从而确保主键的唯一. import java.util.UUID; /** * 工 ...
- Ps钢笔操作技巧
鼠标左键单击=新建锚点:Ctrl+鼠标左键=移动锚点/移动调节点: Alt+鼠标左键=锚点/角点转换: 方向键=微调锚点位置: shift+鼠标左键=新建水平/垂直锚点:Ctrl+Alt+鼠标左键=选 ...