zoj3057 Beans Game---三维DP 博弈

本文介绍了一个名为BeansGame的博弈问题,玩家TT和DD轮流从三堆豆子中取豆子,谁拿到最后一颗豆子谁就获胜。文章提供了两种解决此问题的方法:一种是记忆化搜索方法,但会超出内存限制;另一种是预处理所有状态的方法,能够有效解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题目不难,问题是卡时间卡内存,记忆化搜索不行的,超内存。
 
MLE代码:
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#include<algorithm>
using namespace std;
int dp[301][301][301];
int dfs(int x,int y,int z)
{
    int b[3];
    b[0]=x;b[1]=y;b[2]=z;
    sort(b,b+3);
    x=b[0];y=b[1];z=b[2];
    if(dp[x][y][z]>=0)  return dp[x][y][z];
    for(int i=1;i<=x;i++)
    if(dfs(x-i,y,z)==0)  return dp[x][y][z]=1;

    for(int i=1;i<=y;i++)
    if(dfs(x,y-i,z)==0)  return dp[x][y][z]=1;

    for(int i=1;i<=z;i++)
    if(dfs(x,y,z-i)==0)  return dp[x][y][z]=1;

    for(int i=1;i<=x;i++)
    if(dfs(x-i,y-i,z)==0) return dp[x][y][z]=1;

    for(int i=1;i<=y;i++)
    if(dfs(x,y-i,z-i)==0) return dp[x][y][z]=1;

    for(int i=1;i<=x;i++)
    if(dfs(x-i,y,z-i)==0) return dp[x][y][z]=1;

    return dp[x][y][z]=0;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    dp[0][0][0]=0;
    int a[3];
    while(scanf("%d%d%d",&a[0],&a[1],&a[2])!=EOF)
    {
        sort(a,a+3);
        int ans=dfs(a[0],a[1],a[2]);
        if(ans) puts("1");
        else puts("0");
    }
}

 
http://blog.youkuaiyun.com/acm_cxlove/article/details/7851904
#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值