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
题意:
三维数组分别表示,取三个物品的数量。1.那么之前出现过某两物品数相同,且另一物品数小于当前第三物品数的状态为必胜态,2.如果一物品数相同,且另两物品数大于前面的另外两个物品数,且差相同,则为必胜态,不是这两种必胜态,则为必败态。
正推的效率远不及倒推的效率。
记忆化搜索会炸内存,甚至int都不行,必须用bool。
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#define C 240
#define TIME 10
#define inf 1<<25
#define LL long long
using namespace std;
bool dp[301][301][301]={0};
void Init(){
for(int i=0;i<=300;i++){
for(int j=0;j<=300;j++){
for(int k=0;k<=300;k++){
if(!dp[i][j][k]){
for(int r=1;r+i<=300;r++)
dp[i+r][j][k]=1;
for(int r=1;r+j<=300;r++)
dp[i][j+r][k]=1;
for(int r=1;r+k<=300;r++)
dp[i][j][k+r]=1;
for(int r=1;r+j<=300&&r+i<=300;r++)
dp[i+r][j+r][k]=1;
for(int r=1;r+j<=300&&r+k<=300;r++)
dp[i][j+r][k+r]=1;
for(int r=1;r+k<=300&&r+i<=300;r++)
dp[i+r][j][k+r]=1;
}
}
}
}
}
int main(){
int a,b,c;
Init();
while(cin>>a>>b>>c)
cout<<dp[a][b][c]<<endl;
return 0;
}
本文介绍了一个名为BeansGame的博弈问题,通过三维数组模拟两个玩家轮流从三堆豆子中取豆的过程。文章提供了一段C++代码实现,利用记忆化搜索的方法预先计算所有状态的胜负情况,并详细解释了必胜态和必败态的判断条件。
459

被折叠的 条评论
为什么被折叠?



