C language Memory Search ___play Game (Hdu 4597)

Source: Internet
Author: User

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

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
 
Test instructions: There are two piles of cards, each with the same number of cards, and each card has a number, now a, B, take turns from each of the two piles of cards to draw one card at a time (each extraction can only be extracted from the top or the bottom). After all the cards are requested, the numbers on each card are as large as possible. Suppose AB is smart enough to ask what the sum of the last numbers is. Each group of test data has three rows, the first behavior of the number of cards per pile, the second row and the third row are two pairs of cards corresponding to the number, output a the sum of the last number.

Analysis: This should be the interval DP bar, you can look at the prototype of this problem:
    Other rules are the same, but only a sequence of numbers, but also each can take the left and right at the end of a number, ask how much Alice to get?  
    Only one line of sequence of numbers can use F (i, j) to indicate that the number sequence is still remaining in the interval [I,J] paragraph when you start to take, up to how many numbers

    And this problem just becomes a two-line sequence of numbers, Then you can add two dimensions
    to F (i, J, K, L) on top of the above, which means that the remainder of the first sequence [I,j], the second sequence with the remainder of the interval [k,l] in the case of starting to take, up to how much?
    When facing status F (i, J, K, L), you have four options:
   1. Select the leftmost number of the first row
   2. Select the rightmost number of the first row
  & Nbsp;3. Select the leftmost digit of the second row
   4. Select the rightmost number of the second row
    So, F (i, J, K, L) can be by:
   f (I+1, J, K, L)
  ;  f (i, j-1, K, L)
   f (i, J, K+1, L)
   f (i, J, K, L-1)
    These four states are transferred,  
    Assuming the current state is Alice to choose, then the previous state is the maximum value that Bob chooses,
    in order to make Alice's final and maximum, then choose the top four states the smallest of the turn,
    Set SUM (i, J, K, L) to represent the sums of a sequence [I,j] segment and the sum of the [k,l] segments of the second sequence.

Sum (i, J, K, L)-The last time Bob took the value is equal to the value Alice can get


F (i, J, K, L) = SUM (i, J, K, L)-min{F (I+1, J, K, L), F (i, j-1, K, L), F (i, J, K+1, L), F (i, J, K, L-1)};

On the code:
#include <stdio.h> #include <string.h>int dp[30][30][30][30];int arr1[30],arr2[30],sum1[30],sum2[30]; int max (int a,int b) {if (a>b) return A;return B;} int dfs (int l1,int r1,int l2,int r2) {if (dp[l1][r1][l2][r2]!=-1) return dp[l1][r1][l2][r2];if (l1>r1&&l2> R2) return dp[l1][r1][l2][r2]=0;int ans=0;int sum=0;if (L1<=R1) sum+=sum1[r1]-sum1[l1-1];if (L2<=R2) sum+=sum2[ R2]-sum2[l2-1];if (L1<=R1) {Ans=max (Ans,sum-dfs (L1+1,R1,L2,R2)); Ans=max (Ans,sum-dfs (L1,R1-1,L2,R2));} if (L2<=R2) {Ans=max (Ans,sum-dfs (L1,R1,L2+1,R2)); Ans=max (Ans,sum-dfs (l1,r1,l2,r2-1));} return Dp[l1][r1][l2][r2]=ans;} int main () {int t,i,n;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (i=1;i<=n;i++) {scanf ("%d", &arr1[ I]); sum1[i]=sum1[i-1]+arr1[i];} for (i=1;i<=n;i++) {scanf ("%d", &arr2[i]); sum2[i]=sum2[i-1]+arr2[i];} Memset (Dp,-1,sizeof (DP));p rintf ("%d\n", DFS (1,n,1,n));} return 0;}



C language Memory Search ___play Game (Hdu 4597)

Related Article

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.