HDU 4597 Play Game (memory-based search)

Source: Internet
Author: User



There are two piles of n cards and each card has a score. Alice and Bob take the cards at both ends of the two cards in turn.


Ask Alice to get the highest score;


#include 
 
    #include 
  
     #include   #include 
   
      #include 
    
      #include 
     
        #define M 50  #define LL long long  using namespace std;int n;int dp[M][M][M][M];int x[M],y[M],sum1[M],sum2[M];int dfs(int a,int b,int c,int d){if(dp[a][b][c][d]) return dp[a][b][c][d];if(a>b&&c>d) return 0;int ans1=0,ans2=0;if(a<=b){ans1=max(x[a]+dfs(a+1,b,c,d),x[b]+dfs(a,b-1,c,d));}if(c<=d){ans2=max(y[c]+dfs(a,b,c+1,d),y[d]+dfs(a,b,c,d-1));}return dp[a][b][c][d]=sum1[b]-sum1[a-1]+sum2[d]-sum2[c-1]-max(ans1,ans2);  }int main(){int t;scanf("%d",&t);while(t--){   scanf("%d",&n);memset(dp,0,sizeof(dp));sum1[0]=0;sum2[0]=0;for(int i=1;i<=n;i++){scanf("%d",&x[i]);sum1[i]=sum1[i-1]+x[i];}for(int i=1;i<=n;i++){   scanf("%d",&y[i]);sum2[i]=sum2[i-1]+y[i];}printf("%d\n",sum1[n]+sum2[n]-dfs(1,n,1,n));}return 0;}
     
    
   
  
 


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.