Algorithm design and analysis--polygon game (DP)

Source: Internet
Author: User

1, problem description:   

given the polygons of N vertices, each vertex is labeled with an integer, each edge is labeled with A + (plus) or x (multiply) number, and the n edges are numbered clockwise by 1~n. A polygon with a n=4 vertex is given.

Game rules: (1) First, remove an edge.

(2) then do the following: Select an edge E, the edge has two adjacent vertices, it may be called V1 and V2. The integers marked by the vertices of the V1 and V2 are calculated according to the operation symbol (+ or x) on E, and an integer is given, and the integer is labeled with a new vertex instead of V1 and V2. Continue this operation until there is no edge at the end, that is, there is only one vertex left. The integer of the vertex is called the score of the game (score).

2, problem analysis:

solving the problem can be solved by the optimal substructure property in the dynamic programming .

A clockwise sequence of vertices and edges of the given polygon is op[1],v[1],op[2],v[2],op[3],..., op[n],v[n] whereop[i] represents the operator corresponding to the edge of the I, V[i] represents the value on the first I vertex , i=1~ N.

In the given polygon, starting at vertex I (1<=i<=n), the clockwise chain P (i,j) with a length of J (j vertices in the chain) can be expressed as v[i],op[i+1],..., v[i+j-1], if the last merge operation of this chain occurs at Op[i+s] (1 <=S<=J-1), the chain can be split into two sub-chains P (i,s) and P (i+s,j-s) at Op[i+s].

set m[i,j,0] is the minimum value of the chain P (i,j) merge, and m[i,j,1] is the maximum value . If the optimal merge at Op[i+s] is divided P (i,j) into two lengths less than J of the maximum and minimum value of the sub-chain has been calculated. That

a=m[i,s,0] b=m[i,s,1] c=m[i,s,0] d=m[i,s,1]

(1) When op[i+s]= ' + '

M[i,j,0]=a+c; m[i,j,1]=b+d

(2) When op[i+s]= ' * '

M[I,J,0]=MIN{AC,AD,BC,BD}; M[I,J,1]=MAX{AC,AD,BC,BD}

Because the optimal disconnection position S has 1<=s<=j-1 in the J-1 case. Initial boundary value is m[i,1,0]=v[i] 1<=i<=n M[i,1,1]=v[i] 1<=i<=n

Because the changeable form is closed, in the above calculation, when I+s>n, the vertex i+s is actually numbered (i+s) Modn. The m[i,n,1 calculated as described above] is the maximum score for the first time that the game has been removed from the edge of article I.

The code is as follows:

1 //2015.5.2:--anonymous2#include <string.h>3#include <stdio.h>4 intv[101];5 intN;6 Charop[101];7 intMINF,MAXF;8 intm[101][101][2];9 voidMinmax (intIintSintj)Ten { One     inte[5]; A     inta=m[i][s][0], -b=m[i][s][1], -R= (i+s-1)%n+1, thec=m[r][j-s][0], -d=m[r][j-s][1]; -     if(op[r]=='T') -     { +minf=a+C; -maxf=b+D; +     } A     Else at     { -e[1]=a*C; -e[2]=a*D; -e[3]=b*C; -e[4]=b*D; -minf=e[1]; inmaxf=e[1]; -          for(intk=2; k<5; k++) to         { +             if(minf>E[k]) -minf=E[k]; the             if(maxf<E[k]) *maxf=E[k]; $         }Panax Notoginseng     } - } the intMain () + { AMemset (M,0,sizeof(m)); thescanf"%d",&n); + GetChar (); -      for(intI=1; i<=n; i++) $     { $scanf"%c",&op[i]); -scanf"%d",&v[i]); -m[i][1][0]=V[i]; them[i][1][1]=V[i]; - GetChar ();Wuyi     } the      for(intj=2; j<=n; J + +)//the length of the chain -          for(intI=1; i<=n; i++)//Delete the edge of article I Wu              for(ints=1; s<j; s++)//the location of the disconnection -             { About 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; A             } +     inttemp=m[1][n][1]; the      for(intI=2; i<=n; i++) -     { $         if(temp<m[i][n][1]) thetemp=m[i][n][1]; the     } theprintf"%d\n", temp); the     return 0; -}

Test data:

Input:

4t-7 T 4 x 2 x 5 output:
33

Computational Complexity Analysis:

Similar to the triangulation problem of convex polygons, the above algorithm requires O (N3) computation time.

Algorithm design and analysis--polygon game (DP)

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.