NOIP Passing Notes / Garlic Jun's Castle Tour

pass a note

Topic description

Xiao Yuan and Xiao Xuan are good friends and classmates. They always have endless topics to talk about together. In a quality development activity, the students in the class arranged to make a matrix with m rows and n columns, and Xiaobuchi and Xiaoxuan were arranged at opposite ends of the diagonal of the matrix, so they could not talk directly. Fortunately, they can communicate by passing notes. The note has to be passed to each other through many students. Xiao Yuan sits in the upper left corner of the matrix with coordinates (1, 1), and Xiao Xuan sits in the lower right corner of the matrix with coordinates (m, n). The note passed from Xiaoyuan to Xiaoxuan can only be passed down or to the right, and the note passed from Xiaoxuan to Xiaoyuan can only be passed up or left. During the event, Xiao Yuan wanted to pass a note to Xiao Xuan, and at the same time hope Xiao Xuan would reply to him. Every student in the class can help them pass it, but only once, that is to say, if this person helps Xiaoyuan when handing Xiaoxuan the note, then when Xiaoxuan hands it to Xiaoyuan, he will not. Help again. vice versa. There is one more thing to pay attention to, the favorability of each student in the class is high or low (note: the favorability of Xiaoyuan and Xiaoxuan is not defined, it is represented by 0 when inputting), you can use a natural number from 0 to 100 to indicate that the larger the number, the better the heart. Xiaoyuan and Xiaoxuan hope to find classmates with a high degree of kindness to help pass the note as much as possible, that is, to find two back-and-forth transmission paths, so that the kindness of the students on these two paths is only the highest. Now, please help Xiaoyuan and Xiaoxuan to find these two paths.

enter

The first row has 2 integers m and n separated by spaces, indicating that there are m rows and n columns in the class (1≤m, n≤50) The next m rows are an m*n matrix, the i-th in the matrix The integer in row j and column represents the kindness of the student sitting in row i and column j. The n integers on each line are separated by spaces.

output

A total of one line, including an integer, indicating the maximum value of the sum of the goodwill of the students who participated in passing the note on the two roads back and forth

sample input

3 3
0 3 9
2 8 5
5 7 0

Sample output

34


Problem-solving instructions: dual-thread dp, three-dimensional optimization.

dp[i][j][k][l] means that a point is at (i,j), and the point of the other line is (k,l)! Because the number of steps taken is the same l=i+jk;

dp[i][j][k] reduces one dimension. max (four paths to this state (two points, two ways));

AC code:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h> 
using namespace std;
const int MAXN=55;
int a[MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
int maxd(int a,int b,int c,int d){
	int temp1=max(a,b);
	int temp2=max(c,d);
	temp1=max(temp1,temp2);
	return temp1;
}
int main(){
	int n,m;cin>>n>>m;
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=m;j++){
	  	cin>>a[i][j];
	  }
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=m;j++)
	    for(int k=1;k<i+j&&k<=n;k++){
	    	if(i==k)continue;
	    	dp[i][j][k]=maxd(dp[i][j-1][k],dp[i-1][j][k-1],dp[i][j-1][k-1],dp[i-1][j][k])+a[i][j]+a[k][i+j-k];
		}
	cout<<dp[n][m][n-1]<<endl;
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326037441&siteId=291194637