Nim
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1501   Accepted: 845

Description

Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones on the table (we know the number of stones exactly). We play in turn and at each turn, you or I can remove on to four stones from the heap. You play first and the one who removed the last stone loses.
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

The
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

The output should consist of lines each containing either a one, meaning your team has a winning strategy, or a zero otherwise.

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(博弈树)的更多相关文章

  1. poj 2068 Nim

    Nim POJ - 2068 题目大意:多组数据,两人轮流操作,n轮一循环,给出总石子数和这n轮每次两人能取的石子上限(下限为1).取到最后一颗者输. /* f[i][j]表示在第i轮中一共有j个石子 ...

  2. POJ 2068 Nim#双人dp博弈

    http://poj.org/problem?id=2068 #include<iostream> #include<cstdio> #include<cstring&g ...

  3. POJ 2068 Nim(博弈论)

    [题目链接] http://poj.org/problem?id=2068 [题目大意] 给出两队人,交叉放置围成一圈,每个人能取的石子数有个上限,各不相同 轮流取石头,取到最后一块石头的队伍算输,问 ...

  4. poj 2068 Nim(博弈dp)

    Nim Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1403   Accepted: 791 Description Le ...

  5. poj 2068 Nim 博弈论

    思路:dp[i][j]:第i个人时还剩j个石头. 当j为0时,有必胜为1: 后继中有必败态的为必胜态!!记忆化搜索下就可以了! 代码如下: #include<iostream> #incl ...

  6. POJ 2068 NIm (dp博弈,每个人都有特定的取最大值)

    题目大意: 有2n个人,从0开始编号,按编号奇偶分为两队,循环轮流取一堆有m个石子的石堆,偶数队先手,每个人至少取1个,至多取w[i]个,取走最后一个石子的队伍输.问偶数队是否能赢. 分析: 题目数据 ...

  7. HDU 3404&POJ 3533 Nim积(二维&三维)

    (Nim积相关资料来自论文曹钦翔<从"k倍动态减法游戏"出发探究一类组合游戏问题>) 关于Nim积计算的两个函数流程: 代码实现如下: ][]={,,,}; int N ...

  8. POJ 2975 Nim(博弈论)

    [题目链接] http://poj.org/problem?id=2975 [题目大意] 问在传统的nim游戏中先手必胜策略的数量 [题解] 设sg=a1^a1^a3^a4^………^an,当sg为0时 ...

  9. [原博客] POJ 2975 Nim 统计必胜走法个数

    题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...

随机推荐

  1. 创建Java线程池

    线程池的作用: 线程池作用就是限制系统中执行线程的数量. 根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率不高.用线程池控制线程数量,其他线 ...

  2. c语言中函数的简单介绍

    c语言中函数的介绍: 函数,简单的说就是代码的打包.存放在一个地方,当需要的时候调用. 函数分类: 1.无参无返回值函数 void func() 2.无参有返回值函数  int func() 3.有参 ...

  3. jQuery 事件 方法

    jQuery 事件方法 事件方法触发器或添加一个函数到被选元素的事件处理程序. 下面的表格列出了所有用于处理事件的 jQuery 方法. 方法 描述 bind() 向元素添加事件处理程序 blur() ...

  4. 类库探源——System.Environment

    Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集:   mscorlib.dll 继承关系: 常用属性(字段)和方法: ...

  5. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法

    我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...

  6. C#程序中:如何修改xml文件中的节点(数据)

    要想在web等程序中实现动态的数据内容给新(如网页中的Flash),不会更新xml文件中的节点(数据)是远远不够的,今天在这里说一个简单的xml文件的更新,方法比较基础,很适合初学者看的,保证一看就懂 ...

  7. WPF学习(二)布局与菜单、工具栏

    布局 //表格①Grid//3列 4行的表格   <Grid>    <Grid.ColumDefinitions>             <ColumnDefinti ...

  8. etTimeout与setInterval方法的区别

    etTimeout与setInterval方法的区别 setTimeout()用于设定在指定的时间之后执行对应的函数或代码.,在全局作用域下执行 setTimeout(code,time[,args… ...

  9. 超链接字体颜色设置(通过html/css的设置方法)

    超链接字体颜色设置是通过css来设置,a链接的颜色设置常用的有以下两种,1.超链接a的初始状态颜色,2.超链接字体的鼠标滑过颜色,还有两种病不常用:3.超链接字体的已访问颜色,4.超链接字体在按下鼠标 ...

  10. 如何更有效学习php开源项目的源码

    一.先把源代码安装起来,结合它的文档和手册,熟悉其功能和它的应用方式. 二.浏览源代码的目录结构,了解各个目录的功能. 三.经过以上两步后相信你对这个开源的产品有了一个初步的了解了,那现在就开始分析它 ...