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 ...
随机推荐
- Python学习系列(七)( 数据库编程)
Python学习系列(七)( 数据库编程) Python学习系列(六)(模块) 一,MySQL-Python插件 Python里操作MySQL数据库,需要Python下安装访 ...
- svn服务器端回退版本 (转)
由于误操作,不小心将错误的代码提交到了svn上,于是想在服务器上撤销本次提交,经过尝试,发现进行以下步骤的操作即可彻底删除本次提交: 1.首先找到本次提交后生成的版本号,例如为r224. 2.登录到s ...
- 关于android api 23 +的权限问题
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, n ...
- php写入、追加写入文件的实例
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $t ...
- lnmp php7 安装mysqli扩展 undefined function mysqli_connect()
在用ci框架的时候, https://blog.csdn.net/zqtsx/article/details/8746497 https://blog.csdn.net/move_now/articl ...
- 【转】 Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子
目录(?)[-] 基础小例子 发送Broadcast intent 运行情况 应用间的广播 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog ...
- java 代码,练习ip,主机名的获取方法。InetAddress类
package clientFrame; import java.io.IOException; import java.net.*; public class tai { public static ...
- PTA 银行排队问题之单队列多窗口服务(25 分)
银行排队问题之单队列多窗口服务(25 分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假 ...
- [PHP] - Laravel - CSRF token禁用方法与排除验证csrf_token的url设置
laravel5.1排除验证csrf_token的url设置 <?php namespace App\Http\Middleware; use Illuminate\Foundation\Htt ...
- JavaScript笔记——正则表达式
正则表达式(regular expression)是一个描述字符模式的对象.JavaScript的 RegExp 类 表示正则表达式,而 String 和 RegExp 都定义了使用正则表达式进行强大 ...