HDU - 4597 Play Game (博弈dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597

Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1927    Accepted Submission(s): 1145


 

Problem Description

Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?

 

 

Input

The first line contains an integer T (T≤100), indicating the number of cases. 
Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤10000).

 

 

Output

For each case, output an integer, indicating the most score Alice can get.

 

 

Sample Input

 

2 1 23 53 3 10 100 20 2 4 3

 

 

Sample Output

 

53 105

 

 

很简单的一道题 就是经典博弈区间dp取的层数变成了两层而已

但是因为不是写成递归 两层之间互相有影响 使得判边界的时候写的很糟糕 和自己原本边界写在外面的习惯不一样 记录发。。

#include<bits/stdc++.h>
using namespace std;
int a[111];
int b[111];
int dp[22][22][22][22];
int suma[111];
int sumb[111];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        memset(dp,0,sizeof(dp));
        memset(suma,0,sizeof(suma));
        memset(sumb,0,sizeof(sumb));
        for(int i=1;i<=n;i++)
        {
            suma[i]=suma[i-1]+a[i];
            sumb[i]=sumb[i-1]+b[i];
        }
        for(int lb=0;lb<=n;lb++)
        {
            for(int ib=1;ib+lb-1<=n;ib++)
            {
                int jb=ib+lb-1;
                for(int la=0;la<=n;la++)
                {
                    for(int ia=1;ia+la-1<=n;ia++)
                    {
                        int ja=ia+la-1;
                        if(lb)
                        {
                            dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja]-suma[ia-1]+sumb[jb-1]-sumb[ib-1]-dp[ia][ja][ib][jb-1]+b[jb]);
                            dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja]-suma[ia-1]+sumb[jb]-sumb[ib]-dp[ia][ja][ib+1][jb]+b[ib]);
                        }
                        if(la)
                        {
                            dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja-1]-suma[ia-1]+sumb[jb]-sumb[ib-1]-dp[ia][ja-1][ib][jb]+a[ja]);
                            dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja]-suma[ia]+sumb[jb]-sumb[ib-1]-dp[ia+1][ja][ib][jb]+a[ia]);
                        }

                        //cout <<ia << " " << ja << " " << ib << " " << jb << " " <<dp[ia][ja][ib][jb] <<endl;
                    }
                }
            }
        }
        printf("%d\n",dp[1][n][1][n]);
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值