1006 Pass the note

Source: Internet
Author: User

Difficulty: popularize +/improve

Topic Type: Dynamic planning

Number of submissions: 1

Related knowledge: Multidimensional dynamic regulation

Title Description

Obuchi and Xiao Xuan are good friends and classmates, they always have to talk about endless topics. A quality expansion activities, the class arranged to make a M-row N-column matrix, and Obuchi and small Xuan is arranged at both ends of the matrix diagonal, so they can not directly talk. Fortunately, they can communicate by passing a note. The note to pass through many students to the other hand, Obuchi sitting in the upper left corner of the matrix, coordinates (at a), the small Xuan sits in the bottom right corner of the matrix, coordinates (M,N). From the Obuchi to the small Xuan note can only be passed down or to the right, from the small Xuan to Obuchi note can only be passed up or to the left.

In the activity, Obuchi hope to send a note to the small Xuan, at the same time hope that the small Xuan to reply to him. Every classmate in the class can help them pass, but will only help them once, that is, if the person in the Obuchi hand to the small Xuan Note when help, then in the small Xuan handed to Obuchi when will not help. Vice versa.

There is one more thing to pay attention to, the class each classmate is willing to help a high degree of good and low (note: Obuchi and small Xuan's kindness degree is not defined, input with 0), you can use a 0-100 natural number to express, the greater the number of the more kindness. Obuchi and small Xuan hope as far as possible to find good-hearted students to help pass the note, that is, to find two ways to pass the path, so that the two path to the kindness of the students and the largest. Now, please help Obuchi and Xiao Xuan to find such two paths.

Input/output format

Input format:

The first line of the input file message.in has 2 integers m and n separated by spaces, indicating that there are m rows n columns (1<=m,n<=50) in the class.

The next M-line is a m*n matrix, and the integer in row J of the matrix indicates the kindness of the student sitting in row J of line I. Each row is separated by a space between n integers.

Output format:

The output file message.out a common line that contains an integer that represents the maximum value of the sum of the kindness of the student who is involved in passing the note back and forth on both paths.

Code:

1#include <iostream>2 using namespacestd;3 intm, N;4 intkind[Wuyi][Wuyi];5 intf[Wuyi][Wuyi][Wuyi][Wuyi];6 intMain () {7Cin>>m>>N;8     intI, J, K, L;9      for(i =1; I <= m; i++)Ten          for(j =1; J <=n; J + +) OneCin>>Kind[i][j]; A      for(i =1; I <= m; i++){ -          for(j =1; J <= N; J + +){ -              for(k =1; K <= m; k++){ the                  for(L =1; L <= N; l++){ -                     if((I < m| | J < N) &&i <=k&&j <= k) -                         Continue; -                     intnum =0; +num = max (num, f[i-1][j][k-1][l]); -                     if(I-1!=k&&j!=l-1) +num = max (num,f[i-1][j][k][l-1]); A                     if(i!=k-1&&j-1!=l) atnum = max (num, f[i][j-1][k-1][l]); -num = max (num, f[i][j-1][k][l-1]); -F[I][J][K][L] = num+kind[i][j]+Kind[k][l]; -                 } -             } -         } in     } -cout<<F[m][n][m][n]; to     return 0; +}

Note:

Reference:

Set F[i][j][k][l] to the maximum goodwill and at the position of (I, J), (K, L) from the position of (0, 0) from two non-intersecting lines, then it may have four cases in the previous step:

The first point from the top walk, the second point also from the top, at this time of goodwill and for F[i-1][j][k-1][l] + a[i][j] + a[k][l]

The first point comes from the top, the second point is left to come, at this time the goodwill and for F[I-1][J][K][L-1] + a[i][j] + a[k][l], but at this time should consider whether the point above the first point will coincide with the point to the left of the second point, if coincident is not advisable

The first point from left to go, the second point is from the top, at this time the goodwill and for F[i][j-1][k-1][l] + a[i][j] + a[k][l], but at this time should consider the first point of the left point will coincide with the point above the second point, if coincident is not advisable

The first point from left to go, the second point also from left to come, at this time of goodwill and for F[I][J-1][K][L-1] + a[i][j] + a[k][l]

Take the maximum of four cases.

It is not possible to reach the same point before the end, so (I, J) cannot equal (K, L), plus less than the judgment is because when (I, j) with (K, L) interchange, the maximum goodwill value must be the same, do not have to repeat the calculation

I heard that the absolute problem can be solved by moving the rules (pounder. jpg). I'm going to practice the rules.

1006 Pass the note

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.