• 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 }
    
    
  • 相关阅读:
    Oracle-连接多个字段
    Oracle-like 多条件过滤
    SQL-Union、Union ALL合并两个或多个 SELECT 语句的结果集
    EXCEL-批量删除筛选出的行,并且保留首行
    EXCEL-REPLACE()替换字符串最后几位 删除字符串最后几位
    Oracle-常用表的查询、增加列、删除列、修改列值功能【增删改查】
    Excel-返回列表或数据库中的分类汇总(汇总可以实现要还是不要统计隐藏行功能) subtotal()
    Excel-统计各分数段人数 frequency()
    Excel-给出指定数值的日期 date()
    Class类的理解与获取Class的实例
  • 原文地址:https://www.cnblogs.com/1013star/p/10332191.html
Copyright © 2020-2023  润新知