Linear DP solves the respective selection problem (grid access, ZG and GG paper transfer, paper transfer)

Posted by paruby on Sun, 02 Jan 2022 19:22:03 +0100

Original title link: Grid access
Grid access
Classification: two dimensional can enter a point repeatedly without repeated calculation
Set n × In the square graph of N, we fill some squares with positive integers, while others with the number 0. As shown in the figure below:

Starting from the upper left corner A in the figure, someone can walk down or right until he reaches point B in the lower right corner.

On the way, he can take the number in the square (the square will become the number 0).

This person walked twice from point A to point B, trying to find two such paths to maximize the sum of the numbers.

Input format
The first line is an integer N, representing N × The grid of N.

The next row has three integers. The first is the number of row numbers, the second is the number of column numbers, and the third is the number placed on the row and column.

Row and column numbers start with 1.

A line of "0" indicates the end.

Output format
Outputs an integer representing the maximum sum obtained on the two paths.

Data range
N≤10

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=20;
const int INF=0x3f3f3f3f;
int dp[2*N][N][N],ai[N][N];
//dp[i1][j1][i2][j2] means that all go from (1,1), (1,1) to (i1, j1),
//The maximum value of the path of (i2,j2) is the lattice of two paths only when i1+j1==i2+j2
//It can coincide
// f[k,i1,i2], where k represents the sum of i1+j1, i2+j2
// f[k,i1,i2] indicates that all go from (1,1), (1,1) to (I1, k-i1) respectively
//Maximum value of the path of (i2,k-i2)
void solve()
{
    int n;
    cin>>n;
    int a,b,c;
    while(cin>>a>>b>>c,a||b||c)
        ai[a][b]=c;
    for(int k=2; k<=2*n; k++)
        for(int i1=1; i1<=n; i1++)
            for(int i2=1; i2<=n; i2++)
            {
                int j1=k-i1,j2=k-i2;
                if(j1>=1&&j1<=n&&j2>=1&&j2<=n)
                {
                    int t=ai[i1][j1];
                    if(i1!=i2) t+=ai[i2][j2];
                    int &x=dp[k][i1][i2];
                    x=max(x,dp[k-1][i1-1][i2-1]+t);
                    x=max(x,dp[k-1][i1-1][i2]+t);
                    x=max(x,dp[k-1][i1][i2-1]+t);
                    x=max(x,dp[k-1][i1][i2]+t);
                }
            }
    cout<<dp[2*n][n][n]<<endl;
}
int main()
{
    solve();
    return 0;
}

ZG and GG pass notes
Classification: three-dimensional can enter a point repeatedly without repeated calculation
6204: ZG and GG pass notes
Time limit: 1 Sec memory limit: 512 MB
Title Description
ZG and GG are good friends and classmates. They always have endless topics together. In a quality development activity, the students in the class arranged to make a matrix with M rows and N columns, while ZG and GG were arranged at both ends of the diagonal of the matrix. Therefore, they could not talk directly. Fortunately, they can communicate by passing notes. The note should be passed to each other through many students. ZG sits in the upper left corner of the matrix, coordinates (1,1), and GG sits in the lower right corner of the matrix, coordinates (m,n). The note from ZG to GG can only be transferred downward or to the right, and the note from ZG to GG can only be transferred upward or to the left.
During the activity, ZG hopes to pass a note to GG, and hope GG will reply to him, and then ZG will pass a note to him again. Every student in the class can help them pass, but after helping once, the favor will become 0, that is, if this person helps when ZG hands GG a note, the favor will be 0 when he needs his help again. vice versa.
Another thing to note is that each student in the class is willing to help. They can be expressed by a natural number of 0-10000. The larger the number, the better. ZG and GG hope to find students with a high degree of kindness to help pass the note, that is, to find three transmission paths, so that the sum of the kindness of the students on these three paths is the largest. Now, please help ZG and GG find these three paths.
input
The first row has two integers m and N separated by spaces, indicating that there are m rows and N columns in the class (4 < = m, n < = 20).
The next m row is an m*n matrix. The integer in row i and column j of the matrix represents the kindness of the students sitting in row i and column j. The N integers in each line are separated by spaces.
output
A total of one line, including an integer, represents the maximum value of the sum of the kindness of the students participating in the delivery of the note on the three roads.
Sample input Copy
4 4
4 3 9 6
2 8 5 6
5 7 8 9
4 7 8 3
Sample output Copy
90
Tips
100% of the data meet: 4 < = m, n < = 20.
label

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=60;
int n,m;
int w[N][N];
int f[N*2][N][N][N];
void solve()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            cin>>w[i][j];
 
    for(int k=2; k<=n+m; k++)
        for(int x1=max(1,k-m); x1<=n&&x1<k; x1++)
            for(int x2=max(1,k-m); x2<=n&&x2<k; x2++)
                for(int x3=max(1,k-m); x3<=n&&x3<k; x3++)
                {
                    int t=0;
                    if(x1!=x2&&x1!=x3&&x2!=x3) t=w[x1][k-x1]+w[x2][k-x2]+w[x3][k-x3];
                    else if(x1==x2&&x1!=x3) t=w[x1][k-x1]+w[x3][k-x3];
                    else if(x1==x3&&x1!=x2) t=w[x1][k-x1]+w[x2][k-x2];
                    else if(x2==x3&&x2!=x1) t=w[x1][k-x1]+w[x2][k-x2];
                    else t=w[x1][k-x1];
                    for(int a=0; a<=1; a++)
                        for(int b=0; b<=1; b++)
                            for(int c=0; c<=1; c++)
                            {
                                f[k][x1][x2][x3]=max(f[k][x1][x2][x3],f[k-1][x1-a][x2-b][x3-c]+t);
                            }
                }
 
    cout<<f[n+m][n][n][n]<<endl;
}
int main()
{
    solve();
    return 0;
}

Pass a note
Pass a note
Classification: two-dimensional cannot enter the same point repeatedly
Obuchi and Xiaoxuan are good friends and classmates. They always have endless topics together.

In a quality development activity, the class arranged to sit in a matrix of m rows and n columns, while Obuchi and Xiaoxuan were arranged at both ends of the diagonal of the matrix. Therefore, they could not talk directly.

Fortunately, they can communicate by passing notes.

The note has to be passed to each other through many students. Obuchi sits in the upper left corner of the matrix, coordinates (1,1), and Xiaoxuan sits in the lower right corner of the matrix, coordinates (m,n).

The note from Xiaoyuan to Xiaoxuan can only be passed down or right, and the note from Xiaoxuan to Xiaoyuan can only be passed up or left.

During the activity, Obuchi hopes to pass a note to Xiaoxuan and hope Xiaoxuan will reply to him.

Each student in the class can help them pass, but will only help them once, that is, if this person helps when Xiaoxuan hands Xiaoxuan a note, he will not help when Xiaoxuan hands it to Xiaoyuan, and vice versa.

Another thing to note is that each student in the class is willing to help with varying degrees of favor (Note: the degree of kindness of Obuchi and Xiaoxuan is not defined, and it is expressed by 0 when entering). It can be expressed by a natural number of 0 ∼ 100. The larger the number, the better.

Obuchi and Xiaoxuan hope to find students with a high degree of kindness to help pass the note, that is, to find two transmission paths back and forth, so as to maximize the sum of the kindness of the students on these two paths.

Now, please help Obuchi and Xiaoxuan find these two paths.

Input format
The first row has two integers m and n separated by spaces, indicating that the student matrix has m rows and n columns.

The next m line is an m × n matrix, the integers in row i and column j in the matrix represent the kindness of the students sitting in row i and column j, and the n integers in each row are separated by spaces.

Output format
Output an integer indicating the maximum value of the sum of the kindness of the students involved in passing the note on the two roads back and forth.

Data range
1≤n,m≤50
Input example:
3 3
0 3 9
2 8 5
5 7 0
Output example:
34

Solution: Problem solution
Major changes can be judged as follows:

                        int t = g[i][k - i];
                        if (i != j || k == 2 || k == n + m)
                        {
                            t += g[j][k - j];
                            f[k][i][j] = max(f[k][i][j], f[k - 1][i - a][j - b] + t);
                        }

Topics: Algorithm Dynamic Programming ICPC