HDU 4597 Play Game "Memory Search"

Source: Internet
Author: User

Play GameTime limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 805 Accepted Submission (s): 464


Problem Descriptionalice and Bob are playing a game. There is piles of cards. There is 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 would be added to his total s Core. Alice and Bob are both clever enough, and would pick up cards to get as many scores as possible. 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 AI (1≤ai≤10000). The third line contains N integer bi (1≤bi≤10000).
Outputfor each case, output a integer, indicating the most score Alice can get.
Sample Input
2  1 23 53  

Sample Output
Analysis: A very good memory search problem, let me to the memory of the search and have a new understanding. This problem can also have the interval DP to do, but should say that the memory of the search more appropriate, I only say the memory of the search. First analyze the test instructions, and then combine the code. Each person's current way has four, that is, 4, so there is a total of 4 of the state transfer equation, and the current value of these 4 equations results in the largest value. Because of the two people in the topic is smart, my only difference is who first who, so the two people's card strategy is the same, this is the key place, otherwise can not solve. So you will see my code in the current value is to take the rest of the cards and subtract the next person to draw the best policy when the value, and then go to the 4 in the case of the maximum, here a bit around, or look at the code bar. code example: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define MAXH 30
using namespace Std;
int DP[MAXH][MAXH][MAXH][MAXH];
int SUM1[MAXH],SUM2[MAXH];
int max (int a,int b)
{
Return a>b?a:b;
}
int dfs (int h1,int t1,int h2,int T2)
{
if (dp[h1][t1][h2][t2]!=-1)
return DP[H1][T1][H2][T2];
dp[h1][t1][h2][t2]=0; Equation of state transition in 4
if (H1&LT;=T1)
Dp[h1][t1][h2][t2]=sum1[t1]-sum1[h1-1]+sum2[t2]-sum2[h2-1]-dfs (H1+1,T1,H2,T2);//The sum of the remaining cards minus the next person's optimal strategy value
if (H1&LT;=T1)
Dp[h1][t1][h2][t2]=max (Dp[h1][t1][h2][t2],sum1[t1]-sum1[h1-1]+sum2[t2]-sum2[h2-1]-dfs (H1,T1-1,H2,T2));
if (H2&LT;=T2)
Dp[h1][t1][h2][t2]=max (Dp[h1][t1][h2][t2],sum1[t1]-sum1[h1-1]+sum2[t2]-sum2[h2-1]-dfs (H1,T1,H2+1,T2));
if (H2&LT;=T2)
Dp[h1][t1][h2][t2]=max (Dp[h1][t1][h2][t2],sum1[t1]-sum1[h1-1]+sum2[t2]-sum2[h2-1]-dfs (h1,t1,h2,t2-1)); Optimal in//4
return DP[H1][T1][H2][T2];
}
int main ()
{
int t,n,x;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
sum1[0]=sum2[0]=0;
for (int i=1;i<=n;i++)
{
scanf ("%d", &x);
Sum1[i]=sum1[i-1]+x;
}
for (int i=1;i<=n;i++)
{
scanf ("%d", &x);
Sum2[i]=sum2[i-1]+x;
}
Memset (Dp,-1,sizeof (DP));
printf ("%d\n", DFS (1,n,1,n));
}
return 0;
}

HDU 4597 Play Game "Memory Search"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.