Beans Game(博弈 | | DP)zoj 3057

Beans Game


Time Limit: 5 Seconds Memory Limit: 32768 KB


There are three piles of beans. TT and DD pick any number of beans from any pile or the same number from any two piles by turns. Who get the last bean will win. TT and DD are very clever.

Input

Each test case contains of a single line containing 3 integers a b c, indicating the numbers of beans of these piles. It is assumed that 0 <= a,b,c <= 300 and a + b + c > 0.

Output

For each test case, output 1 if TT will win, ouput 0 if DD will win.

Sample Input

1 0 0
1 1 1
2 3 6

Sample Output

1
0
0

博弈题;相对于取石子游戏那题多了一堆;(但,有一点不一样,就是范围!那么就简单多了)题目大意:有三堆豆子a,b,c(a+b+c<=300)。TT和DD轮流从其中一堆拿走任意个豆子或从其中的两种拿走同样多的豆子,最后一个拿完的获胜。

算法分析:

典型的威佐夫博弈问题是两堆,而此题为三堆。

原本以为这题是要找规律,但是找了一个小时没找到。百度解题报告,发现原来只要逆向推即可。采用递推比较快。

p[i][j][k] = 0表示状态为i,j,k时是必败态,p[i][j][k] = 1为必胜态。

从必败态往上推,能从必败态推出来的就是必胜态,赋值为1,如果是必胜点就不用往上推了。

p[i][j][k]初始化为必败点,如果该点不能从必败点转移过来,该点就是必败点。

由于必败点比较少所以可以节省时间。

时间: 2024-08-06 03:50:07

Beans Game(博弈 | | DP)zoj 3057的相关文章

UVA 1558 - Number Game(博弈dp)

UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次可以选一个数字,然后它的倍数,还有其他已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp记忆化去求解,要输出方案就枚举第一步即可,状态转移过程中,选中一个数字,相应的变化写成一个函数,然后就是普通的博弈问题了,必胜态之后必有必败态,必败态之后全是必胜态 代码: #include <stdio.h> #include <string.h> const int N = 105

UVA 1557 - Calendar Game(博弈dp)

UVA 1557 - Calendar Game 题目链接 题意:给定一个日期,两个人轮流走,每次可以走一月或者一天,问最后谁能走到2001.11.4这个日子 思路:记忆化搜索,对于每个日期,如果下两个状态有一个非必胜态,那么这个状态是必胜态,如果后继状态都是必胜态,那么该状态为必败态 代码: #include <stdio.h> #include <string.h> const int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31,

UVA 1559 - Nim(博弈dp)

UVA 1559 - Nim 题目链接 题意:一开始有s个石子,2n个人轮流取石子,每个人有个最大能取数目,2n个人奇数一队,偶数一队,取到最后一个石子的队输,问谁赢 思路:记忆化搜索,每个人取的时候对应的后继状态如果有一个必败态,则该状态为必胜态,如果都是必胜态,则该状态为必败态 代码: #include <stdio.h> #include <string.h> int n, s, m[25], dp[25][10005]; int dfs(int now, int state

[dp] zoj 3769 Diablo III

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 Diablo III Time Limit: 2 Seconds      Memory Limit: 65536 KB Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new expansion of Diablo I

[dp] zoj 3740 Water Level

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5122 Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especially the West Lake. Recently, the water level of the West Lake got lower and lower

[递推dp] zoj 3747 Attack on Titans

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170 Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newf

[dp] zoj 3682 E - Cup 3

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3682 E - Cup 3 Time Limit: 3 Seconds      Memory Limit: 65536 KB The 2012 Europe Cup was over and Spain won the Championship. The fans of Spain want to hold some activities to celebra

UVa 10891 (博弈+DP) Game of Sum

最开始的时候思路就想错了,就不说错误的思路了. 因为这n个数的总和是一定的,所以在取数的时候不是让自己尽可能拿的最多,而是让对方尽量取得最少. 记忆化搜索: d(i, j)表示原序列中第i个元素到第j个元素构成的子序列,先手取数能够得到的最大值. sum(i, j) 表示从第i个元素到第j个元素的和 因为要让对手获得最小的分数,所以状态转移方程为: d(i, j) = sum(i, j) - min{d(枚举所有可能剩给对手的序列), 0(0代表全部取完)} s数组保存a中前i个元素的和,这样s

codeforcesB - A Lot of Games 博弈+dp

题意:给你n个字符串,给你一个序列,两个人轮流取一个字符使得现有的字符串是n个字符串里面的前缀,最后谁不能取谁就输掉这局,但是他们要玩K局,谁在K局赢了就等于赢了一整场比赛. 解题思路:字典树找是否有 必输 或者 必赢 的策略,如果同时有必赢或者必输的策略,那必定是first赢,如果只有必赢,那只需要讨论K奇偶,如果只有必输,那一定是second 解题代码: 1 // File Name: 1.cpp 2 // Author: darkdream 3 // Created Time: 2014年