• Play Game (博弈DP)


    Play Game

     HDU - 4597

    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?

    InputThe 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 a i (1≤a i≤10000). The third line contains N integer b i (1≤bi≤10000).OutputFor 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 
    题意: 
       Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个
       只能从其中一个序列,选择两端中的一个拿走。他们都希望可以拿到尽量大
       的数字之和,并且他们都足够聪明,每次都选择最优策略。Alice先选择,问
       最终Alice拿到的数字总和是多少?
    题解:
      dp[l1][r1][l2][r2]代表面对当前两个序列分别为l1~r1 l2~r2时,先手能拿到数字总和的最大值。而dp[l1][r1][l2][r2]是由dp[l1+1][r1][l2][r2],dp[l1][r1-1][l2][r2],dp[l1][r1][l2+1][r2],dp[l1][r1][l2][r2-1]四个状态转移来的。

    这是第一个样例的dfs图

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int casen;
     7 int n;
     8 int dp[25][25][25][25];
     9 int sum1[30];
    10 int sum2[30];
    11 int dfs(int l1,int r1,int l2,int r2)
    12 {
    13     if(dp[l1][r1][l2][r2]!=-1)
    14         return dp[l1][r1][l2][r2];
    15     dp[l1][r1][l2][r2]=0;
    16     int sum=sum1[r1]-sum1[l1-1]+sum2[r2]-sum2[l2-1];
    17     if(l1<=r1)
    18         dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-dfs(l1+1,r1,l2,r2));
    19     if(l1<=r1)
    20         dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-dfs(l1,r1-1,l2,r2));
    21     if(l2<=r2)
    22         dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-dfs(l1,r1,l2+1,r2));
    23     if(l2<=r2)
    24         dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-dfs(l1,r1,l2,r2-1));    
    25     return dp[l1][r1][l2][r2]; 
    26 }
    27 int main()
    28 {
    29     int x;
    30     scanf("%d",&casen);
    31     while(casen--)
    32     {
    33         memset(sum2,0,sizeof(sum2));
    34         memset(sum1,0,sizeof(sum1));
    35         memset(dp,-1,sizeof(dp));
    36         scanf("%d",&n);
    37         for(int i=1;i<=n;i++)
    38         {
    39             scanf("%d",&x);
    40             sum1[i]=sum1[i-1]+x;
    41         }
    42         for(int i=1;i<=n;i++)
    43         {
    44             scanf("%d",&x);
    45             sum2[i]=sum2[i-1]+x;
    46         }
    47         int ans=dfs(1,n,1,n);
    48         printf("%d
    ",dp[1][n][1][n]);
    49     }
    50 }
    
    
  • 相关阅读:
    【转载】深入浅出VA函数
    oracle数据库怎么创建数据库实例
    2.4 OpenEuler中C语言中的函数调用测试(选做)
    OpenEuler 中C与汇编的混合编程(选做)
    程序运行
    改进ls的实现(课下作业)
    学习笔记12
    实验四 Web服务器1socket编程
    实验四 Web服务器2
    学习6
  • 原文地址:https://www.cnblogs.com/1013star/p/10332191.html
Copyright © 2020-2023  润新知