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 ...
随机推荐
- Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 mysql远程连接问题
在用Navicat for MySQL远程连接mysql的时候,出现了 Lost connection to MySQL server at ‘reading initial communicatio ...
- C++的显示转换
利用显示转换使得我们可以很容易发现它们,因为通过名字就能找到: static_cast 用于“良性”和“适度良性”转换,包括不用强制转换 const_cast 对“const”和“volatil ...
- 说说http请求
为什么做web前端要了解http标准?因为浏览器要从服务端获取网页,网页也可能将信息再提交给服务器,这其中都有http的连接.web系统既然和http链接有瓜葛,你就必须去了解它.我将从一下几个方面讲 ...
- wordpress4.0.1源码学习和摘录--项目设置
1.静态变量日期 define( 'MINUTE_IN_SECONDS', 60 ); define( 'HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS ); def ...
- PHP实现简易的模板引擎
PHP实现简易的模板引擎 1.MVC简介 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式(详情自己百度): 1. Mode ...
- 小爬虫。爬取网站多页的通知标题并存取在txt文档里。
爬取网页中通知标题的内容展示: this is 1 page!<精算学综合>科目考试参考大纲2016年上半年研究生开题报告评议审核结果公示[答辩]2016下半年研究生论文答辩及学位评定 ...
- ntpath join(path, *paths) 发生UnicodeDecodeError的Bug的解决方案
最近在学习用Python开发web,使用的是web框架,在win8.1平台下安装SAE和Flask的时候总是在ntpath.py这个UnicodeDecodeError模块报错. 84行 result ...
- 用三或四个个div标签实现工字效果
使用重构的方式制作出一个如下图的水平.垂直都居中,短边为50px,长边为150px的红色“工”字. a) 使用3个div完成 <!DOCTYPE html><html lang=&q ...
- LightOj_1079 Just another Robbery
题目链接 题意: 抢银行(这个背景最爱了), 有n家银行, 每家银行抢劫被抓的概率是p[i],你认为当你被抓的概率低于P的时候是安全的. 问, 你最多能抢劫到多少money. 思路: 抽象成背包问题, ...
- Maven镜像配置
镜像是为了提供更快的服务 如图:X就认为是Y的一个镜像. 编辑settings.xml配置中央仓库镜像: <settings> ... <mirrors> <mirror ...