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

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

题目:有两个队从石堆中去石子,每一个队有n个人,每一个人每次取石子的数量有限制,然后

取最后一块的人所在的队伍输。

如今问第一队(成员为1,3,5,7...)是否可以赢得比赛。

思路 :dp[i][j]表示第i个人取,石堆剩余 j 块石头。当j为0的时候,没有石头,这时候是胜,为1。

后继中有必败态的为必胜态。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int M=1<<14;
const int maxn=50; int dp[maxn][M],a[maxn],tol,n; void initial()
{
memset(dp,-1,sizeof(dp));
} void input()
{
scanf("%d",&tol);
for(int i=0;i<2*n;i++) scanf("%d",&a[i]);
} int DP(int x,int num)
{
if(dp[x][num]!=-1) return dp[x][num];
if(num==0) return dp[x][num]=1;
dp[x][num]=0;
for(int i=1;i<=a[x] && i<=num;i++)
if(!DP((x+1)%(2*n),num-i))
dp[x][num]=1;
return dp[x][num];
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
initial();
input();
printf("%d\n",DP(0,tol));
}
return 0;
}

poj 2068 Nim(博弈dp)的更多相关文章

  1. POJ 2068 Nim#双人dp博弈

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

  2. poj 2068 Nim

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

  3. 2018山东省赛 G Game ( Nim博弈 && DP )

    题目链接 题意 : 给出 N 堆石子,每次可以选择一堆石子拿走任意颗石子,最后没有石子拿的人为败者.现在后手 Bob 可以在游戏开始前拿掉不超过 d 堆的整堆石子,现在问你有几种取走的组合使得 Bob ...

  4. POJ 2234 Nim博弈

    思路: nim博弈裸题 xor一下 //By SiriusRen #include <cstdio> using namespace std; int n,tmp,xx; int main ...

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

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

  6. POJ 2068 Nim(博弈论)

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

  7. poj 2068 Nim(博弈树)

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

  8. poj 2068 Nim 博弈论

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

  9. 博弈dp入门 POJ - 1678 HDU - 4597

    本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...

随机推荐

  1. Asp.NetMVC和WebForm的请求生命周期

    1.MVC的执行过程 用户  ---->控制器--->ViewData进行传值--->视图(进行显示) 2.Controller中的Action 主要进行的作用: 1.处理用户的请求 ...

  2. 《SQL基础教程》

    Product表 CREATE TABLE Product (product_id CHAR(4) NOT NULL, product_name VARCHAR(100) NOT NULL, prod ...

  3. Pycharm里面使用PIL库之后,为什么调用Image的方法不能弹出代码提示,怎样能让代码提示弹出?

    之前也碰到了这个问题,安装了pillow后没有代码提示,最后查了半天,发现问题原来非常简单,解决方法也很无厘头. 之所以没有代码提示,仅仅是因为Pycharm没法判断Image.open()返回的对象 ...

  4. hdu 1548 升降梯

    题目大意:有一个升降机,它有两个按钮UP和DOWN,给你一些数i表示层数,并且每层对应的Ki,如果按UP按钮,会从第i层升到第i+Ki层:如果按了DOWN则会从第i层降到第i-Ki层:并规定能到的层数 ...

  5. 微服务的发现与注册--Eureka

    目录 服务提供者.服务消费者.服务发现组件三者之间的关系 Eureka 简介 Eureka Server Eureka Client 编写Eureka Server 将微服务注册到Eureka Ser ...

  6. win10定时执行php脚本

    转自http://www.cnblogs.com/wenhainan/p/6962089.html 第一步:确认windows上是否配置好了php环境变量,我用xampp安装的lamp环境,默认已经配 ...

  7. python全栈开发day34-线程Thread

    一.昨日内容回顾 1. 概念和理论 进程是计算机资源分配最小单位 进程三状态.同步.异步.阻塞.非阻塞 2. 进程的创建 实例化.自建类run,start,join,terminate,daemon等 ...

  8. 第三章XML简介

    概念:XML:提供数据交换.系统配置.内容管理等的功能,可跨平台.跨网络.跨程序的数据描述方式.XSL:依靠XPath定位,提供显示模板,且专门为了显示XML文件信息的语言.CSS(层叠样式表):在网 ...

  9. i春秋 “百度杯”CTF比赛 十月场 web题 Backdoor

    0x00: 打开题目,题目中告诉我们这题是文件泄露. 0x01: 通过扫描目录,发现可以扫到的有3个文件 index.php flag.php robots.txt 但是浏览flag.php它告诉我们 ...

  10. Python HTTP 请求时对重定向中的 cookie 的处理

    首先说明一下,我使用的是 Python3 的 urllib,但 Python2.x 同理(使用 urllib2). 想用脚本去登录一个网站.和很多网站一样,该网站使用 cookie 来保存会话信息.这 ...