Play Game_dfs

Posted 阿宝的锅锅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于Play Game_dfs的主要内容,如果未能解决你的问题,请参考以下文章

ForegroundService没有从片段开始?

如何以毫秒为单位获取javaFX音频片段的长度[关闭]

向 Google Play 游戏排行榜提交分数并显示新排名

在节点上的函数内部运行方法

应用易受 Intent 重定向问题的影响

unity3d 如何用GUI按钮播放动画脚本怎么写