1. 程式人生 > >Play Game (博弈DP)

Play Game (博弈DP)

return ice mage utf card play rds bob height

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)