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 ...
随机推荐
- Windows I/O模型、同步/异步、阻塞/非阻塞
转载自:http://www.cppblog.com/tx7do/articles/5954.html 同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实 ...
- RelativeLayout相对布局 安卓布局技巧
http://blog.csdn.net/nieweiking/article/details/38417317 RelativeLayout相对布局 相对布局 RelativeLayout 允许子元 ...
- CSS之关于clearfix--清除浮动
一,什么是.clearfix 你只要到Google或者Baidu随便一搜"css清除浮动",就会发现很多网站都讲到"盒子清除内部浮动时可以用到.clearfix" ...
- 在jsp中用EL 表达来获取表单中的参数
在一个JSP页面转到另一个JSP页面时,对表单中的参数用EL表达式提取为: <form action="sampleJsp.jsp" method="po ...
- JAVA zip解压 MALFORMED 错误
最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...
- NSAttributedString用法
以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILabel,“元/位”一个UILabel.今天翻看以前的工程,command点进UITextField中看到[attrib ...
- kettle不能正常自动获取字段
Unable to close prepared statement after determining SQL layoutYou have an error in your SQL syntax ...
- 【感悟】看Hyouka的感想 (1)
最近偶然从B站看到了<冰菓>这个(个人觉得是推理)番 我突然觉得自己曾经做的一些行为欠妥 有才能者的不自知,是对无才能者的讽刺 举个例子就是:即当别人说你很牛的时候,你却说你只 ...
- Introduction to object
1 Declarations VS definitions (Page 81) declarations: This function or variable exists somew ...
- 子网/ip/子网掩码
IP地址由网络地址和主机地址组成 而现在IP由“子网掩码”通过子网网络地 址细分出 A,B,C类更小的网络.这种方式 实际上就是将原来的A类,B类,C类等分类 中的的主机地址部分用作子网地址,可以 将 ...