UVA 10891 Game of Sum

Source: Internet
Author: User
Tags integer numbers

Original question:
This is a and the player game. Initially there is n integer numbers in an array and players 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 is taken from the array by the players. The
Point of each player was calculated by the summation of the numbers, which he had taken. Each player
Tries to achieve more points from other. If Both players play optimally and player A starts the game
Then what much more point can player A get than player B?
Input
The input consists of a number of cases. Each case starts with a line specifying the integer n (0 <
N≤100), the number of elements in the array. After that, n numbers is given for the game. Input is
Terminated by a line where n = 0.
Output
For each test case, print a number, which represents the maximum difference that the first player
Obtained after playing this game optimally.
Sample Input
4
4-10-20 7
4
1 2 3 4
0
Sample Output
7
10
Effect:
Give you a bunch of numbers, and then A and B two people to make a game. The rules of the game are two people take turns to fetch the number, each can be taken from any one of the number, and each can take a number of consecutive numbers, but not at both ends. If A is selected first, and both use the optimal strategy, ask the last initiator and the difference between the two is how much.

#include <bits/stdc++.h>
using namespace std;
FStream in,out;
int n,sum[101];
int d[101][101];
BOOL vis[101][101];
int dp (int i,int j)
{
    if (Vis[i][j])
        return d[i][j];
    Vis[i][j]=true;
    int tmp=0;
    for (int k=i+1;k<=j;k++)
        tmp=min (TMP,DP (k,j));
    for (int k=j-1;k>=i;k--)
        tmp=min (TMP,DP (i,k));
    d[i][j]=sum[j]-sum[i-1]-tmp;
    return d[i][j];
}
int main ()
{
    Ios::sync_with_stdio (false);
    while (Cin>>n,n)
    {
        memset (d,0,sizeof (d));
        memset (sum,0,sizeof (sum));
        memset (vis,false,sizeof (Vis));
        for (int i=1;i<=n;i++)
        {
            int t;
            cin>>t;
            sum[i]=sum[i-1]+t;
        }
        DP (1,N);
        cout<<d[1][n]*2-sum[n]<<endl;
    }
    return 0;
}

 #include <bits/stdc++.h> using namespace std;//fstream in,out; int dp[101][101],sum[
101],f[101][101],g[101][101],n;
    int main () {Ios::sync_with_stdio (false);
    int i,j,l;
        while (cin>>n,n) {memset (dp,0,sizeof (DP));
        sum[0]=0;
            for (i=1;i<=n;i++) {int t;
            cin>>t;
            Sum[i]=sum[i-1]+t;
        f[i][i]=g[i][i]=dp[i][i]=t;
                } for (l=1;l<n;l++) {for (i=1;i+l<=n;i++) {j=i+l;
                int tmp=0;
                Tmp=min (Tmp,f[i+1][j]);
                Tmp=min (Tmp,g[i][j-1]);
                dp[i][j]=sum[j]-sum[i-1]-tmp;
                F[i][j]=min (Dp[i][j],f[i+1][j]);
            G[i][j]=min (Dp[i][j],g[i][j-1]);
    }} cout<<dp[1][n]*2-sum[n]<<endl;
} return 0; }

Answer:
This topic refer to the Blog
Http://www.lxway.com/4001256026.htm (recommended)
Http://www.tuicool.com/articles/fEjAzu

This question did not think out, later read other people's blog understand. But there are some thinking results, just get the topic when did not think of the state of the amount of what, so first hand simulation, to see if it can be a little inspiration.
1. For a game game, everyone uses the optimal strategy, then two people's behavior and decision-making is the same, then the transfer equation in the establishment of the time does not need to consider how to do the two people respectively.
2. For a given pile of numbers, the decision-making process is also very simple, that is, take the left or take the right. And how many questions to take.
When I analyzed the 2 situation, I figured out that the state can be represented by dp[i][j] as the optimal process of taking I to J number.
Next, how to establish a state transfer, and if you decide the problem, but unfortunately did not think out.
Before the analysis of two people are using the optimal strategy, then the problem can be arbitrarily defined as a good. Look at other people's blogs are defined as the state of the initiator. So how is the state shifted, and here's a game problem called the Minimax algorithm. This algorithm borrowed from the above reference a blog summed up the sentence is: the sum is certain, so the higher the score of a person, the lower the score of the other person. So we can analyze that the other person scored the lowest score m in all the best options available, and Sum-m is my top score.
Here to understand, everyone uses the optimal strategy, consider dp[i][j] to take I to J number of the initiator of the best strategy, must be dp[i][k] and dp[k][j] (that is, the sub-state of dp[i][j) before the optimal strategy of the decision to choose the most rotten, and then use Sum[i , j] (I to J) minus, is the person's optimal strategy.
So the state transfer equation is dp[i][j]=sum[i][j]-min (dp[k][j],dp[i][k]), can be used for memory search, can also be recursive. If you use recursion to calculate, the above reference blog with space Exchange time complexity, very good ideas.

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.