UVA 10404 Bachet's Game(dp + 博弈?)
Problem B: Bachet's Game
Bachet's game is probably known to all but probably not by this name. Initially there are n stones on the table. There are two players Stan and Ollie, who move alternately. Stan always starts. The legal moves consist in removing at least one but not more than k stones from the table. The winner is the one to take the last stone.
Here we consider a variation of this game. The number of stones that can be removed in a single move must be a member of a certain set of m numbers. Among the m numbers there is always 1 and thus the game never stalls.
Input
The input consists of a number of lines. Each line describes one game by a sequence of positive numbers. The first number is n <= 1000000 the number of stones on the table; the second number is m <= 10 giving the number of numbers that follow; the last m numbers on the line specify how many stones can be removed from the table in a single move.
Input
For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly.
Sample input
20 3 1 3 8
21 3 1 3 8
22 3 1 3 8
23 3 1 3 8
1000000 10 1 23 38 11 7 5 4 8 3 13
999996 10 1 23 38 11 7 5 4 8 3 13
Output for sample input
Stan wins
Stan wins
Ollie wins
Stan wins
Stan wins
Ollie wins
题意:给定n个石头,和m种去除石头的方式,每种方式可以去除一定量的石头, 现在Stan(简称S),Ollie(简称O),S先手,O后手,每次每个人能选择一种去除石头的方式,谁去除最后一堆谁就赢了。要求出必胜之人是谁。
思路:一开始没头绪,以为是博弈。没想出好的思路。由于n很大,去遍历状态肯定超时。之后看了别人的题解,才发现不错的dp思路:用一个dp数组记录,对于先手者能取到的记录为1,后手者为0,初始都为0,遍历1到n,如果dp[i]为0,说明上一手是后手取得,这样先手就能取,把dp[i]变为1,由于是从1 到 n,这样每个状态记录时,前面的都已经记录好了,所以是可行的。这样最后只需要判断dp[n]是1,还是0,就可以判断是先手胜还是后手胜了。
状态转移方程为:if (i - move[j] >= 0 && !dp[i - move[j]]) dp[i] = 1。
代码:
#include <stdio.h>
#include <string.h> int n, m, move[15], dp[1000005], i, j; int main() {
while (~scanf("%d", &n)) {
memset(dp, 0, sizeof(dp));
scanf("%d", &m);
for (i = 0; i < m; i ++) {
scanf("%d", &move[i]);
}
for (i = 1; i <= n; i ++)
for (j = 0; j < m; j ++) {
if (i - move[j] >= 0 && !dp[i - move[j]]) {
dp[i] = 1;
break;
}
}
if (dp[n])
printf("Stan wins\n");
else
printf("Ollie wins\n");
}
return 0;
}
UVA 10404 Bachet's Game(dp + 博弈?)的更多相关文章
- uva 10404 Bachet's Game(完全背包)
题目连接:10404 - Bachet's Game 题目大意:由一堆石子, 给出石子的总数, 接下来由stan和ollie两个人玩游戏,给出n, 在给出n种取石子的方法(即为每次可取走石子的数量), ...
- UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...
- codevs 1421 秋静叶&秋穣子(树上DP+博弈)
1421 秋静叶&秋穣子 题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一 起,自然会想到烤红薯 ...
- hdu6199 gems gems gems dp+博弈
/** 2017 ACM/ICPC Asia Regional Shenyang Online 解题报告 题目:hdu6199 gems gems gems 链接:http://acm.hdu.edu ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- POJ 2068 NIm (dp博弈,每个人都有特定的取最大值)
题目大意: 有2n个人,从0开始编号,按编号奇偶分为两队,循环轮流取一堆有m个石子的石堆,偶数队先手,每个人至少取1个,至多取w[i]个,取走最后一个石子的队伍输.问偶数队是否能赢. 分析: 题目数据 ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVA 10891 区间DP+博弈思想
很明显带有博弈的味道.让A-B最大,由于双方都采用最佳策略,在博弈中有一个要求时,让一方的值尽量大.而且由于是序列,所以很容易想到状态dp[i][j],表示序列从i到j.结合博弈中的思想,表示初始状态 ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
随机推荐
- Visio中添加、移动或删除形状上的连接点的方法
Visio中添加.移动或删除形状上的连接点的方法 利用Visio画图时,学会使用连接点能使你的画图质量和速度大幅度提高.下面在Visio2010中,以一个例子讲述如何使用连接点. 一. 准备 1. 打 ...
- DLP底座(威创定制)
品牌:威创 型号:BC06730-1000 生产商:广东威创视讯科技股份有限公司 1.DLP底座说明 DLP底座由威创统一定制,确保了整套系统的完整性和可靠性.材质为钢结构,根据淄川地下管线中心的现场 ...
- 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)
原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
- javascript 判断微信浏览器
原文:javascript 判断微信浏览器 用js判断当前环境是否是是微信内置浏览器有两个方法: 1.判断useragent 2.判断是否支持微信内置浏览器才支持的一些方法,比如WeixinJSBri ...
- 与众不同 windows phone (2) - Control(控件)
原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...
- STL 之 queue、priority_queue 源代码剖析
/* * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute an ...
- 使用URLConnection提交请求
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和URL之间的通信连接.程序可以通过URLConnection实例向该URL发送请求,读取URL ...
- NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用
看来非常多网上关于日期的处理,今天.昨天.前天.后天.一周等,满心欢喜的拿着去验证结果总是不尽如人意,看别别人的代码看的脑涨.自己就写一个简单的,自己来用,以后用其它的方法,我会在完好,再次记录.以方 ...
- DelphiXE 显示GIF动画
DelphiXE可以直接显示GIF动画,不需要第三方控件的支持.只要引用GifImg单元即可. uses GIFImg; procedure TForm1.FormCreate(Sender: TOb ...
- 深入 CSocket 编程之阻塞和非阻塞模式
有时,花上几个小时阅读.调试.跟踪优秀的源码程序,能够更快地掌握某些技术关键点和精髓.当然,前提是对这些技术大致上有一个了解. 我通过几个采用 CSocket 类编写并基于 Client/Server ...