zoukankan      html  css  js  c++  java
  • Easy Game(记忆化搜索)

    You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains Nspace separated integers. You may assume that no number will contain more than 4 digits.

    Output

    For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

    Sample Input

    2

    4

    4 -10 -20 7

    4

    1 2 3 4

    Sample Output

    Case 1: 7

    Case 2: 10

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #define Inf 0x3f3f3f3f
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    
    int dp[205][205];
    int sum[205];
    int n;
    int dfs(int l,int r)
    {
        if(dp[l][r]!=-Inf)
        {
            return dp[l][r];
        }
        if(l>r)
        {
            return 0;
        }
        int ans=-Inf;
        for(int t=1;t<=n;t++)
        {
            if(l+t<=r+1)
            ans=max(ans,sum[l+t-1]-sum[l-1]-dfs(l+t,r));
        }
        for(int t=1;t<=n;t++)
        {
            if(r-t>=l-1)
            ans=max(ans,sum[r]-sum[r-t]-dfs(l,r-t));    
        }
    //    cout<<l<<" "<<r<<" "<<ans<<endl;
        return dp[l][r]=ans;
    }
    int main()
    {
       int T;
       cin>>T;
       int cnt=1;
       while(T--)
       {
           scanf("%d",&n);
           for(int t=0;t<=n;t++)
           {
               for(int j=0;j<=n;j++)
               {
                   dp[t][j]=-Inf;
            }
        }
           memset(sum,0,sizeof(sum));
           int x;
           for(int t=1;t<=n;t++)
           {
               scanf("%d",&x);
               dp[t][t]=x;
               sum[t]=sum[t-1]+x;
        }
        dfs(1,n);
        printf("Case %d: %d
    ",cnt++,dp[1][n]);
       }
       return 0;
    }
  • 相关阅读:
    创业公司必备的5款团队协作工具
    测试人必备:国内外最好用的6款Bug跟踪管理系统
    BUG克星:几款优秀的BUG跟踪管理软件
    千亿级SaaS市场:企业级服务的必争之地
    如何使用iClap创建普通批注
    详析手游圈从业人员必须知道的行业术语
    企业级服务元年:iClap高效解决手游更新迭代问题
    手游精品时代,iClap参会TFC高效解决手游问题
    全新办公方式,iClap引领企业级服务新浪潮
    vue自学小demo----前端
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11248746.html
Copyright © 2011-2022 走看看