hdu 4597 Play Game

经典区间dp,两摞牌,分别可以从每一摞牌上面和下面取一张牌

#include 
#include 
int dp[23][23][23][23];
int arr[2][23];
int max(int x,int y)
{
    return x>y?x:y;
}
int dfs(int x1,int y1,int x2,int y2)
{
    if(x1>y1&&x2>y2) return 0;
    if(dp[x1][y1][x2][y2]) return dp[x1][y1][x2][y2];
    int v=0,sum=arr[0][y1]-arr[0][x1-1]+arr[1][y2]-arr[1][x2-1];

    if(x1<=y1)
    {
        v=max(v,sum-dfs(x1+1,y1,x2,y2));
        v=max(v,sum-dfs(x1,y1-1,x2,y2));
    }
    if(x2<=y2)
    {
        v=max(v,sum-dfs(x1,y1,x2+1,y2));
        v=max(v,sum-dfs(x1,y1,x2,y2-1));
    }
    return dp[x1][y1][x2][y2]=v;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int t,n,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        arr[0][0]=arr[1][0]=0;
        for(int i=0; i<2; ++i)
            for(int j=1; j<=n; ++j)
            {
                scanf("%d",&x);
                arr[i][j]=x+arr[i][j-1];
            }
        memset(dp,0,sizeof(dp));
        printf("%d\n",dfs(1,n,1,n));
    }
    return 0;
}


你可能感兴趣的:(hdu)