hdu 4597 Play Game (记忆化搜索 区间dp)

 

 

#include<cstdio>

#include<cstring>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

int dp[30][30][30][30];

int vis[30][30][30][30];

int a[2][30],sum[2][30];

int dfs(int i,int j,int k,int l)

{

    if(vis[i][j][k][l]==1) return dp[i][j][k][l];

    int minn1,minn2,minn;

    vis[i][j][k][l]=1;



    if(i>k&&j>l)

    {

        return 0;

    }

    else

    {

        minn1=minn2=1e9;

        if(i<=k)

        {

          minn1=min(dfs(i+1,j,k,l),dfs(i,j,k-1,l));

        }

        if(j<=l)

        {

          minn2=min(dfs(i,j+1,k,l),dfs(i,j,k,l-1));

        }

        minn=min(minn1,minn2);

        //printf("%d...\n",minn);

    }

    dp[i][j][k][l]=sum[0][k]-sum[0][i-1]+sum[1][l]-sum[1][j-1]-minn;

    return dp[i][j][k][l];



}

int main()

{

    int t,n;

    int i,j,k;

    scanf("%d",&t);

    while(t--)

    {

        memset(vis,0,sizeof(vis));

        scanf("%d",&n);

        for(i=0;i<2;i++)

        {

            sum[i][0]=0;

            for(j=1;j<=n;j++)

            {

                scanf("%d",&a[i][j]);

                sum[i][j]=sum[i][j-1]+a[i][j];

            }

        }

        printf("%d\n",dfs(1,1,n,n));

    }

    return 0;

}

 

你可能感兴趣的:(game)