Blue Book title [brush] && dp pass notes

Topic Portal

Subject description:

Obuchi also are good friends and classmates Xiaoxuan, they are together there is always endless talk about the topic. A Quality Development activities, classmates made arrangements for a matrix of m rows and n columns, while Obuchi and Xiaoxuan are arranged at both ends of the diagonal of the matrix, therefore, they can not talk directly. Fortunately, they can communicate by passing notes. To note passed through the hands of many other students, Obuchi sitting in the upper left corner of the matrix, the coordinate (1,1), sitting in the lower right corner Xiaoxuan matrix of coordinates (m, n). Down or right can only be transferred from the paper Hsuan spread Obuchi, Hsuan only be passed up from the paper or transmitted Obuchi left.

In the activity, the Obuchi Hsuan want to pass a piece of paper, and hope Xiaoxuan give him a reply. Each student in the class can help them deliver, but will only help them once, that if this person Xiaoxuan note handed in to help when Obuchi, then the time will not Xiaoxuan handed Obuchi and then to help. vice versa.

One more thing to note, the class each student willing to help high and low degree of goodwill (note: the degree and kind Obuchi Xiaoxuan not defined, with 0 input), you can use a natural number 0-100 to represent, the greater the number the better the heart. Obuchi and Xiaoxuan want to find the highest possible degree of good intentions to help students pass notes that the two passed back and forth to find the path so that the extent of this kind of only two students and a maximum path. Now, would you please help Obuchi and Hsuan find such two paths.


analysis:

First, we can be transformed into the two lines starting from the top left to bottom right, the maximum data path through the non-repetition and the resulting.

It is easy to think of such an idea:
f [ x 1 ] [ y 1 ] [ x 2 ] [ y 2 ] ( x 1 , y 1 ) ( x 2 , y 2 ) f [x1] [y1] [x2] [y2] represents the end of a note in a first (x1, y1), the second (x2, y2) and the maximum

Data for this question, this way of course be provided too. But we can not therefore meet, like this there will be a lot of redundancy, to the best extent possible.

Since the paper is at the same time pass, then the sum of their ranks should want to wait (push easy to get), we can try to get along with it:
f [ i ] [ x 1 ] [ x 2 ] i ( ( 1 , 1 ) 0 x 1 x 2 f [i] [x1] [x2] for the sum of row and column I (assuming the ranks of (1,1) is zero), the first note in the row x1, x2 in the second row and the maximum

After this set of columns can be determined according to a row, the transfer is performed


Code

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[201][201];
int f[201][51][51];

int main(){
    scanf("%d %d",&n,&m);
    for (int i=1;i<=n;i++)
      for (int j=1;j<=m;j++) scanf("%d",&a[i][j]);
    for (int i=0;i<=100;i++)
      for (int j=1;j<=min(i+1,n);j++)
        for (int k=1;k<=min(i+1,n);k++){
		    int x1=j,x2=k,y1=i+2-x1,y2=i+2-x2;
		    if (x1==x2 && y1+1==y2+1) f[i+1][x1][x2]=max(f[i+1][x1][x2],f[i][x1][x2]+a[x1][y1+1]);
		    else f[i+1][x1][x2]=max(f[i+1][x1][x2],f[i][x1][x2]+a[x1][y1+1]+a[x2][y2+1]); //右 右
		    if (x1==x2-1 && y1==y2+1) f[i+1][x1+1][x2]=max(f[i+1][x1+1][x2],f[i][x1][x2]+a[x1+1][y1]);
		    else f[i+1][x1+1][x2]=max(f[i+1][x1+1][x2],f[i][x1][x2]+a[x1+1][y1]+a[x2][y2+1]); //下 右
		    if (y1==y2 && x1+1==x2+1) f[i+1][x1+1][x2+1]=max(f[i+1][x1+1][x2+1],f[i][x1][x2]+a[x1+1][y1]);
		    else f[i+1][x1+1][x2+1]=max(f[i+1][x1+1][x2+1],f[i][x1][x2]+a[x1+1][y1]+a[x2+1][y2]); //下 下 
		    if (x1==x2+1 && y1+1==y2) f[i+1][x1][x2+1]=max(f[i+1][x1][x2+1],f[i][x1][x2]+a[x1][y1+1]);
		    else f[i+1][x1][x2+1]=max(f[i+1][x1][x2+1],f[i][x1][x2]+a[x1][y1+1]+a[x2+1][y2]); //右 下 
			 
		}
		printf("%d",f[n+m-2][n][n]);
		return 0;
}

Guess you like

Origin blog.csdn.net/huang_ke_hai/article/details/90606569