HDU 4111 Alice and Bob (博弈+记忆化搜索)
题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上。
析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的不是1的堆全部合并起来并全部拿完所以要的操作,
要注意的是 ,一共有这几种情况。
1.a 为0,说明没有一堆的,全是另一种操作,这一种是看操作个数,如果是奇数,先手胜。
2.a 为 1,说明只有只有一堆 为1的, 这个是先手必胜,因为如果 b是偶数,那么就可以先合并,如果是 b 是奇数,就是直接拿掉。
3.b 为 1,说明最后那个也只剩下一个了,要把 b 放到 a 上。
3.从 a 中拿走一个。
4.从 b 中拿走一个。
5.合并 a 中的一个石子到 b。
6.合并 a 中的两个石子,然后就不再是一个了,就得放到 b 上。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int mod = 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int dp[55][50*maxn]; int dfs(int a, int b){
int &ans = dp[a][b];
if(ans != -1) return ans;
if(0 == a) return ans = b & 1;
if(1 == a) return ans = 1;
if(1 == b) return ans = dfs(a+1, b-1); //take the second to the one
if(!dfs(a-1, b)) return ans = 1; //from the one select one
if(b > 0 && !dfs(a, b-1)) return ans = 1; //from the second select one
if(b > 0 && !dfs(a-1, b+1)) return ans = 1; //unit the one to the second
if(a > 1 && !dfs(a-2, b ? b+3 : b+2)) return ans = 1; //unit the two from the one to the second
return ans = 0;
} int main(){
int T; cin >> T;
memset(dp, -1, sizeof dp);
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
int a = 0, b = 0;
for(int i = 0; i < n; ++i){
int x;
scanf("%d", &x);
if(1 == x) ++a;
else b += x + 1;
}
printf("Case #%d: %s\n", kase, dfs(a, b-1) ? "Alice" : "Bob");
}
return 0;
}
HDU 4111 Alice and Bob (博弈+记忆化搜索)的更多相关文章
- BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)
转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...
- UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)
题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu 4111 Alice and Bob(中档博弈题)
copy VS study 1.每堆部是1的时候,是3的倍数时输否则赢: 2.只有一堆2其他全是1的时候,1的堆数是3的倍数时输否则赢: 3.其他情况下,计算出总和+堆数-1,若为偶数,且1的堆数是偶 ...
- HDU 4597 Play Game (DP,记忆化搜索,博弈)
题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略 ...
- HDU 4960 Another OCD Patient(记忆化搜索)
HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...
- CodeForces 918D MADMAX(博弈+记忆化搜索)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
随机推荐
- css处理最后一个li
.relatebar li{width:98px;height:146px;padding:5px;float:left;border-left:1px solid #ccc;} .relatebar ...
- ArcGIS_Lisence安装步骤
1.双击lisence.exe文件 2.下一步 3.关闭 4.下一步 5.下一步 6.下一步 7.安装 8.完成 9.OK
- implicit declaration of function 'init_MUTEX'
'init_MUTEX' 在新版本系统上已被淘汰 改用sema_init(sem, 1);
- (转)Oracle存储过程中的事务
本文转载自:http://www.cnblogs.com/linjiqin/archive/2011/04/18/2019990.html 1.事务用于确保数据的一致性,由一组相关的DML语句组成,该 ...
- 【转】JMeter基础之——录制脚本
Jmeter 是一个非常流行的性能测试工具,虽然与LoadRunner相比有很多不足,比如:它结果分析能力没有LoadRunner详细:很它的优点也有很多: ● 开源,他是一款开源的免费软件,使用它你 ...
- 【转】Jmeter和LR上传文件和下载
背景: 在某个申请业务中,需要上传附件文件,然后才能提交该申请 遇到的问题: 1, 在使用Jmeter或者LR进行录制时,无法录制到上传文件的请求,只能通过Fiddler能查看到该请求 2, 因为 ...
- 圆周率的现代计算机求法(C语言) Lebal:research
C语言求圆周率π 公式法1 #include <stdio.h> #include <math.h> int main(){ float term,result=1; int ...
- mysql表的增删改查
一.表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 二.创建表 cr ...
- springboot成神之——RestTemplate访问Rest
本文介绍RestTemplate访问Rest demo package com.springlearn.learn; import java.util.Arrays; import org.sprin ...
- 解决webpack环境变量NODE_ENV跨平台兼容性问题
为什么要用cross-env插件 学过webpack的人都知道,webpack可以使用NODE_ENV=development或者NODE_ENV=production这样的方式来设置全局变量 这样的 ...