码迷,mamicode.com
首页 > 其他好文 > 详细

Play Game (博弈DP)

时间:2019-01-28 23:53:09      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:info   ring   序列   most   lan   input   origin   nes   clu   

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\n",dp[1][n][1][n]);
49     }
50 }

 

Play Game (博弈DP)

标签:info   ring   序列   most   lan   input   origin   nes   clu   

原文地址:https://www.cnblogs.com/1013star/p/10332191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!