Polygon game (DP), game dp

Source: Internet
Author: User

Polygon game (DP), game dp
Description

A polygon game is a single player game.NPolygon composed of vertices. Each vertex is assigned an integer, and each edge is assigned an operator "+" or "*". All edges are sequentially rounded up from 1NId.

In step 2 of the game, delete an edge.

SubsequentN-Perform the following operations in Step 1:

(1) Select an edgeEAndETwo Connected verticesV1AndV2;

(2) Replacing edges with a new vertexEAnd the two vertices connected by E.V1AndV2. The VertexV1AndV2The integer through the edgeEThe result obtained from the operation on is assigned to a new vertex.

Finally, all edges are deleted and the game ends. The game score is the integer on the remaining vertices.

Problem: Calculate the highest score for a given polygon.W(-1, 231 <W<231 ).

Input

The first line is a separate integer.N(3 ≤N≤ 18), indicating the number of vertices of a polygon (also the number of edges ). NextNLine. Each line contains an operator ("+" or "*") and an integer.V[I] (-10 <V[I] <10), indicatingIOperators andINumber of vertices.

Output

The output has only one integer, indicating the highest score.W.

Sample Input

3
+ 2
* 3
+ 1

Sample Output

9

#include<string.h>#include<stdio.h>#include<iostream>#define MAX 102using namespace std;int v[MAX];char op[MAX];int n,minf,maxf;int m[MAX][MAX][2];void minMax(int i,int s,int j){    int e[5];    int a=m[i][s][0],        b=m[i][s][1],        r=(i+s-1)%n+1,        c=m[r][j-s][0],        d=m[r][j-s][1];    if(op[r]=='+')    {        minf=a+c;        maxf=b+d;    }    else    {        e[1]=a*c;        e[2]=a*d;        e[3]=b*c;        e[4]=b*d;        minf=e[1];        maxf=e[1];        for(int k=2; k<5; k++)        {            if(minf>e[k])                minf=e[k];            if(maxf<e[k])                maxf=e[k];        }    }}int polyMax(){    for(int i=1;i<=n;i++)        for(int j=2;j<=n;j++){            m[i][j][0]=1000000;            m[i][j][1]=-1000000;        }    for(int j=2; j<=n; j++)        for(int i=1; i<=n; i++)            for(int s=1; s<j; s++)            {                minMax(i,s,j);                if(m[i][j][0]>minf)                    m[i][j][0]=minf;                if(m[i][j][1]<maxf)                    m[i][j][1]=maxf;            }    int temp=m[1][n][1];    for(int i=2; i<=n;i++)        if(temp<m[i][n][1]) temp=m[i][n][1];    return temp;}int main(){    memset(m,0,sizeof(m));    cin >> n;    getchar();    for(int i=1;i<=n;i++)    {        cin >> op[i] >> v[i];        getchar();        m[i][1][0]=m[i][1][1]=v[i];    }    cout << polyMax() <<endl;    return 0;}

 

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.