Play Game_dfs

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[l1][r1][l2][r2]表示在a堆还剩l1到r1和b堆还剩l2到r2能取得的最大值。

下面只要考虑从两堆的头尾取四种情况下,哪种的价值最多,sum-上一轮对方拿到的价值

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=25;
int  a[N],b[N];
int dp[N][N][N][N];

int dfs(int l1,int r1,int l2,int r2,int sum)
{
    int mx=0;
    if(l1>r1&&l2>r2) return 0;
    if(dp[l1][r1][l2][r2]) return dp[l1][r1][l2][r2];
    if(l1<=r1)
    {
        mx=max(mx,sum-dfs(l1+1, r1,l2, r2,sum-a[l1]));
        mx=max(mx,sum-dfs(l1,r1-1, l2, r2,sum-a[r1]));
    }
    if(l2<=r2)
    {
        mx=max(mx,sum-dfs(l1, r1,l2+1, r2,sum-b[l2]));
        mx=max(mx,sum-dfs(l1,r1, l2, r2-1,sum-b[r2]));
    }
    dp[l1][r1][l2][r2]=mx;
    return mx;
}
int main()
{
    int t,n,sum;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            sum+=b[i];
        }
        memset(dp, 0, sizeof(dp));
        int ans=dfs(1,n,1,n,sum);
        cout<<ans<<endl;
        
    }
    return 0;
}
原文地址:https://www.cnblogs.com/iwantstrong/p/6367543.html