# Dynamic programming --- example 6. Polygon game

#### 1, Title Description

Polygon game is a single player game. At the beginning, there is a polygon composed of n vertices. Each vertex is given an integer value, and each edge is given an operator "+" or "*". All edges are sequentially numbered with integers from 1 to n.

Step 1 of the game, delete an edge.
The next n-1 step is as follows:
(1) Select an edge e and two vertices V1 and V2 connected by E;
(2) Replace edge E and the two vertices V1 and V2 connected by E with a new vertex. Assign the result obtained from the integer values of vertices V1 and V2 through the operation on edge E to the new vertex.

Finally, all edges are deleted and the game is over. The score of the game is the integer value on the remaining vertices.
Problem: calculate the highest score for a given polygon.

example:

#### 2, Problem solving ideas

##### 1. Optimal substructure properties

In the given polygon, starting from vertex i(1 ≤ I ≤ n), the clockwise chain p(i, j) with length J (there are j vertices in the chain) can be expressed as v[i], op[i+1],..., v[i+j-1].
If the last merging operation of this chain occurs at op[i+s] (1 ≤ s ≤ j-1), the chain can be divided into two sub chains p(i, s) and p(i+s, j-s) at op[i+s].
– examples

Let m1 be the value obtained by any combination of sub chains p(i, s), and a and b are the minimum and maximum values obtained in all possible combinations, respectively.
m2 is the value obtained by any combination of p(i+s, j-s), while c and d are the minimum and maximum values obtained in all possible combinations, respectively.
According to this definition, there are a ≤ m1 ≤ b, c ≤ m2 ≤ d

• When op[i+s] = '+', there is obviously a+c ≤ m ≤ b+d
• When op[i+s] = '*', min{ac, ad, bc, bd} ≤ m ≤ max{ac, ad, bc, bd}

In other words, the maximum and minimum values of the main chain can be obtained from the maximum and minimum values of the sub chain.

##### 2. Recursive solution

It can be seen from the previous analysis that in order to find the maximum value of chain merging, the maximum value and minimum value of sub chain merging must be found at the same time.
Let m[i,j,0] be the minimum value of chain p(i,j) merging, and m[i,j,1] be the maximum value.
If p(i,j) is divided into two sub chains p(i,s) and p(i+s,j-s) with length less than j at op[i+s]
For the convenience of narration, note a=m[i, s,0], b=m[i, s,1], c=m[i+s,j-s,0], d=m[i+s,j-s,0]

• When op[i+s] = '+', m[i,j,0]=a+c, m[i,j,1]=b+d

Combining the above two points, the maximum value of p(i,j) disconnected at op[i+s] is maxf(i,j,s) and the minimum value is minf(i,j,s), then:

Since there are j-1 cases where the optimal disconnection position s is 1 ≤ s ≤ j-1, it can be seen that:

The initial boundary value is obviously m[i,1,0]=v[i], m[i,1,1]=v[i], 1 ≤ I ≤ n.

The code is as follows:

```// Polygon game
#include<bits/stdc++.h>
using namespace std;
#define NMAX 100
int N, m[NMAX+1][NMAX+1][2], v[NMAX+1];  //m[i][j][0], the starting vertex is I, and the number of vertices is J (chain length)
char op[NMAX+1];

void MinMax(int n, int i, int s, int j, int &minf, int &maxf);
int Polymax(int n, int &p);
int main()
{
int p;
cout<<"Please enter the number of polygon vertices: "<<endl;
cin>>N;
for(int i=1; i<N; i++)
{
cin>>v[i];
m[i][1][0]=v[i];//initialization
m[i][1][1]=v[i];
cin>>op[i];
}
cout<<"Polygon game first delete"<<p<<"Strip edge,The result is:"<<Polymax(N, p)<<endl;
return 0;
}

void MinMax(int n, int i, int s, int j, int &minf, int &maxf) //There are n vertices in total. Starting from vertex i, there are j vertices. The disconnection position is r=(i+s-1)%n + 1, the minimum value minf and the maximum value maxf
// This function is used to find the minimum and maximum values when the starting vertex is i, the number of vertices (chain length) is j and the disconnection position is r
{
int e[5];
int a=m[i][s][0], b=m[i][s][1];
int r=(i+s-1)%n + 1;//The actual vertex number at the polygon break position
int 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 r=2; r<N; r++)
{
if(minf>e[r]) minf = e[r];
if(maxf<e[r]) maxf = e[r];
}
}
}
int PolyMax(int n, int &p)
{
int minf, maxf;
for(int j=2; j<=n; j++)
{
for(int i=1; i<=n; i++)
{
for(int s=1; s<j; s++)
{
MinMax(n,i,s,j,minf,maxf);
if(m[i][j][0]>minf) m[i][j][0] = minf;  //Update array continuously
if(m[i][j][1]<maxf) m[i][j][1] = maxf;
}
}
}
int temp = m[1][n][1];
int p = 1;
for(int i=2; i<=n; i++)
if(temp<m[i][n][1])
{
temp = m[i][n][1];
p = i;
}
return temp;
}
```

This article refers to my teacher Bi Fangming's courseware "algorithm design and analysis"
Welcome to my personal blog - George's programming cabin , work hard with me for the big factory offer!

Posted on Mon, 01 Nov 2021 20:16:11 -0400 by Ned