uva 10404 Bachet's Game (完全背包+博弈)
来源:互联网 发布:网络侦探攻略 编辑:程序博客网 时间:2024/06/03 19:10
uva 10404 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
题目大意:给出石头的总数sum, 和取石头的方法(一次能取多少石头)n,接下来是n种取石头的方式。在双方都想赢的状况下,问谁能最后拿走全部石头(先手还是后手)。
解题思路:主体是完全背包的思路。dp数组有{0, 1}两种状况,1代表当前i个石头先取者必胜,0代表当前i个石头先取者必败,状态转移方程是:dp[i−num[j]]==0−−>dp[i]=1 。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#define N 1000005#define M 15using namespace std;typedef long long ll;int num[M], dp[N];int main() { int sum, n; while (scanf("%d %d", &sum, &n) == 2) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { scanf("%d", &num[i]); } for (int i = 1; i <= sum; i++) { for (int j = 0; j < n; j++) { if (i - num[j] >= 0 && !dp[i - num[j]]) { //该取石头的方式可行 且 取完石头之后剩余的石头不能一次取完。 dp[i] = 1; break; } } } if (dp[sum]) printf("Stan wins\n"); else printf("Ollie wins\n"); } return 0;}
- uva 10404 Bachet's Game (完全背包+博弈)
- UVa 10404 Bachet's Game / 完全背包
- uva 10404 Bachet's Game(完全背包)
- Uva 10404-Bachet's Game(博弈)
- dp+博弈 uva-10404-Bachet's Game
- UVA 10404 - Bachet's Game 组合博弈
- UVA 10404 Bachet's Game(dp + 博弈?)
- UVa 10404 Bachet's Game (DP&博弈)
- uva 10404 Bachet's Game(dp 博弈)
- UVA - 10404 Bachet's Game 博弈
- UVA 题目10404 - Bachet's Game(DP+博弈)
- UVa 10404 - Bachet's Game 博弈+动态规划
- uva 10404 - Bachet's Game(DP)
- uva 10404 Bachet's Game
- UVA 10404 Bachet's Game
- UVa 10404 - Bachet's Game
- UVa 10404 - Bachet's Game
- uva 10404 - Bachet's Game
- Android 存储
- 设计模式学习003——工厂模式(工厂方法、抽象工厂),简单工厂【也叫静态工厂】
- Composer 常用功能
- 【c语言】自己编写一遍折半查找
- Prim's minimum spanning tree algorithm
- uva 10404 Bachet's Game (完全背包+博弈)
- 关于html
- 字符串的比较
- 字符串处理函数(string.h)
- C++中const关键字用法详解及实例和源码下载(三)
- C++和C# WebService相互调用
- Spring源码解析——如何阅读源码
- android中Gallery的应用开发-制作自己的相片集
- 【SICP归纳】4 模式匹配和以规则为基础的代换