poj 2068 Nim(博弈树)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1501 | Accepted: 845 |
Description
In this game, you have a winning strategy. To see this, you first
remove four stones and leave 96 stones. No matter how I play, I will end
up with leaving 92 - 95 stones. Then you will in turn leave 91 stones
for me (verify this is always possible). This way, you can always leave
5k+1 stones for me and finally I get the last stone, sigh. If we
initially had 101 stones, on the other hand, I have a winning strategy
and you are doomed to lose.
Let's generalize the game a little bit. First, let's make it a team
game. Each team has n players and the 2n players are seated around the
table, with each player having opponents at both sides. Turn around the
table so the two teams play alternately. Second, let's vary the maximum
number of stones each player can take. That is, each player has his/her
own maximum number of stones he/she can take at each turn (The minimum
is always one). So the game is asymmetric and may even be unfair.
In general, when played between two teams of experts, the outcome of
a game is completely determined by the initial number of stones and the
maximum number of stones each player can take at each turn. In other
words, either team has a winning strategy.
You are the head-coach of a team. In each game, the umpire shows
both teams the initial number of stones and the maximum number of stones
each player can take at each turn. Your team plays first. Your job is,
given those numbers, to instantaneously judge whether your team has a
winning strategy.
Incidentally, there is a rumor that Captain Future and her officers
of Hakodate-maru love this game, and they are killing their time playing
it during their missions. You wonder where the stones are? Well, they
do not have stones but do have plenty of balls in the fuel containers!
Input
input is a sequence of lines, followed by the last line containing a
zero. Each line except the last is a sequence of integers and has the
following format.
n S M1 M2 . . . M2n
where n is the number of players in a team, S the initial number of
stones, and Mi the maximum number of stones ith player can take. 1st,
3rd, 5th, ... players are your team's players and 2nd, 4th, 6th, ... the
opponents. Numbers are separated by a single space character. You may
assume 1 <= n <= 10, 1 <= Mi <= 16, and 1 <= S < 2^13.
Output
Sample Input
1 101 4 4
1 100 4 4
3 97 8 7 6 5 4 3
0
Sample Output
0
1
1
Source
【思路】
博弈
构造博弈树,一个局面必胜当且仅当后继局面有至少一个必败局面,一个局面必败当且仅当后继局面都为必胜局面。
记忆化搜索即可。
【代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = 1e4; int f[][N],a[];
int n,m; int dfs(int r,int tot) {
if(r==*n+) r=;
int &ans=f[r][tot];
if(ans!=-) return ans;
if(tot==) return ans=;
if(tot<=a[r]) return ans=;
FOR(i,,a[r])
if(!dfs(r+,tot-i)) return ans=;
return ans=;
} int main() {
while(scanf("%d",&n)== && n) {
scanf("%d",&m);
FOR(i,,*n) scanf("%d",&a[i]);
memset(f,-,sizeof(f));
if(dfs(,m)) puts("");
else puts("");
}
return ;
}
poj 2068 Nim(博弈树)的更多相关文章
- poj 2068 Nim
Nim POJ - 2068 题目大意:多组数据,两人轮流操作,n轮一循环,给出总石子数和这n轮每次两人能取的石子上限(下限为1).取到最后一颗者输. /* f[i][j]表示在第i轮中一共有j个石子 ...
- POJ 2068 Nim#双人dp博弈
http://poj.org/problem?id=2068 #include<iostream> #include<cstdio> #include<cstring&g ...
- POJ 2068 Nim(博弈论)
[题目链接] http://poj.org/problem?id=2068 [题目大意] 给出两队人,交叉放置围成一圈,每个人能取的石子数有个上限,各不相同 轮流取石头,取到最后一块石头的队伍算输,问 ...
- poj 2068 Nim(博弈dp)
Nim Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1403 Accepted: 791 Description Le ...
- poj 2068 Nim 博弈论
思路:dp[i][j]:第i个人时还剩j个石头. 当j为0时,有必胜为1: 后继中有必败态的为必胜态!!记忆化搜索下就可以了! 代码如下: #include<iostream> #incl ...
- POJ 2068 NIm (dp博弈,每个人都有特定的取最大值)
题目大意: 有2n个人,从0开始编号,按编号奇偶分为两队,循环轮流取一堆有m个石子的石堆,偶数队先手,每个人至少取1个,至多取w[i]个,取走最后一个石子的队伍输.问偶数队是否能赢. 分析: 题目数据 ...
- HDU 3404&POJ 3533 Nim积(二维&三维)
(Nim积相关资料来自论文曹钦翔<从"k倍动态减法游戏"出发探究一类组合游戏问题>) 关于Nim积计算的两个函数流程: 代码实现如下: ][]={,,,}; int N ...
- POJ 2975 Nim(博弈论)
[题目链接] http://poj.org/problem?id=2975 [题目大意] 问在传统的nim游戏中先手必胜策略的数量 [题解] 设sg=a1^a1^a3^a4^………^an,当sg为0时 ...
- [原博客] POJ 2975 Nim 统计必胜走法个数
题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...
随机推荐
- Handler 原理分析和使用之HandlerThread
前面已经提到过Handler的原理以及Handler的三种用法.这里做一个非常简单的一个总结: Handler 是跨线程的Message处理.负责把Message推送到MessageQueue和处理. ...
- (转)VS自带工具:dumpbin的使用
有时候我们想查看一个exe引用了哪些动态库,或者我们想看某个动态库包含哪些接口函数,这个时候可以使用dumpbin.exe工具: 1.输入Dumpbin -imports calldll.exe查看它 ...
- iOS 正则表达式-判断邮箱、手机号
判断是否是邮箱 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[ ...
- Lua与C/C++交互问题
初学lua,遇到注册C/C++交互函数问题 在lua与C/C++交互时,C/C++的注册Lua函数若是一个有返回类型(压栈)而不是获取类型的时候应该返回1而不是返回0,否则会出现在Lua中值为nil( ...
- 测试functional的bind以及相关功能
注:在VS2010 UPDATE1下测试通过 /*测试functional的bind以及相关功能*/ #include <iostream> #include <functional ...
- 《paste命令》-linux命令五分钟系列之二十
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- 指令发email
win7下指令发送email:(telnet:不为内部指令时控制面板 -> 程序和功能 -> 打开或关闭Windows功能,如下“telnet客户端”) telnet smtp.sina. ...
- 帝国cms在任意位置调用指定id的栏目名称和链接
注意,这个代码无须放在灵动标签中,直接写入模板相应的位置就行了.[1]调用栏目名称: <?=$class_r[栏目ID]['classname']?> 示例:<?=$class_ ...
- Linux下glui 的安装,以及错误解决
下载源文件: http://sourceforge.net/projects/glui/ 2. 解压源文件 3. 用terminal进入glui-2.36/src文件 4. make 5. make之 ...
- 局部视图(partial)
局部视图(partial) 原文:Partial Views作者:Steve Smith翻译:张海龙(jiechen).刘怡(AlexLEWIS)校对:许登洋(Seay).何镇汐.魏美娟(初见) AS ...