zoj3057 Beans Game---三维DP 博弈

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.csdn.net/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;
}


你可能感兴趣的:(c,bean,input,each,output,Numbers)