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 ...
随机推荐
- MAC泛洪攻击
先来解释一下啥是泛洪攻击 交换机里有一张专门记录MAC地址的表,为了完成数据的快速转发,该表具有自动学习机制:泛洪攻击即是攻击者利用这种学习机制不断发送不同的MAC地址给交换机,充满整个MAC表,此时 ...
- Docker Commands
Docker Commands 安装,以Ubuntu 14.04.3为例 apt-get install docker.io 注意安装之前需要更新系统 列出曾经存在的容器 docker ps -a 列 ...
- Python 正则表达式、re模块
一.正则表达式 对字符串的操作的需求几乎无处不在,比如网站注册时输入的手机号.邮箱判断是否合法.虽然可以使用python中的字符串内置函数,但是操作起来非常麻烦,代码冗余不利于重复使用. 正则表达式是 ...
- 如何读懂statspack报告
前言:这篇文章是我从网上找到的,但可惜不知道是哪位大侠写(译)的,因此这里无法注明了.仔细看了看,这篇文章对初学者应该很有帮助,写的比较详细,通俗易懂,因此整理一下,便于阅读:内容略有调整,不单做调整 ...
- Java跨域问题的处理
1,JavaScript由于安全性方面的考虑,不允许页面跨域调用其他页面的对象,那么问题来了,什么是跨域问题? 答:这是由于浏览器同源策略的限制,现在所有支持JavaScript的浏览器都使用了这个策 ...
- Maven如何发布项目到一个Tomcat中
首先,在本地tomcat的conf/tomcat-users.xml 中配置一个user,准备让maven接入时使用: <role rolename="admin-gui"/ ...
- Codeforces 798D - Mike and distribution(二维贪心、(玄学)随机排列)
题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:从长度为n的序列A和序列B中分别选出k个下表相同的数要求,设这两个序列中k个数和分别为 ...
- Java容器---Collection接口中的共有方法
1.Collection 接口 (1)Collection的超级接口是Iterable (2)Collection常用的子对象有:Map.List.Set.Queue. 右图中实现黑框的ArrayLi ...
- Cookie 与 网络通信
通信协议是什么?(ftp.http.https.file...) 交流的规则 举例: 汉语.英语.法语.德语 (百度百科:通信协议) 通信协议是指双方实体完成通信或服务所必须遵循的规则和约定.通过通信 ...
- python中的any和all函数
any和all函数是判断一组数据真假性的综合结果.以下摘选自Stackoverflow. ------------------ 分割线开始 ----------------- any any will ...