Luo Gu P1006 pass notes explanations

P1006 pass notes

Title 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 on the two paths of students and maximum. Now, would you please help Obuchi and Hsuan find such two paths.

Input Format

The input file, the first line has two space-separated integers m and n, represents a class of m rows and n columns.

The next line is an m \ (m \ times n \) matrix, the integer matrix row i and column j represents the student sitting kind of degree ii row j-th column. Separated by spaces between each row of n integers.

Output Format

Total output file line contains an integer indicating the road back and forth two students involved in the maximum degree of well-intentioned piece of paper and transfer of.

Sample input and output

Input # 1

3 3
0 3 9
2 8 5
5 7 0

Output # 1

34

Description / Tips

【limit】

30% of the data satisfies: \ (. 1 \ Le m, n-\ Le 10 \)

100% data satisfies: \ (. 1 \ Le m, n-\ Le 50 \)

NOIP 2008 to improve the Group III title

[Thinking]

Similar grid access to this questions
is to go twice
but this is from Obuchi to Xiaoxuan again, and again Xiaoxuan to Obuchi
but may look to take Obuchi Obuchi to Xiaoxuan Xiaoxuan twice or twice
so eliminating the need for a lot of trouble

I thought then fetching analog squares
F (I, J, k, l)
I, J represents the first pass to reach the position, k, l denotes the second pass reaches the position
of this question and access different squares the place is, the
grid may take a few repeat a point to go back although it may be small but is the result of
but passing notes This question can only go once a point
which requires a special treatment
can last in the enumeration is to re-cycle l enumerated from the beginning of the j + 1, l because it is impossible to enumerate the first pass through the point
can then be pressed into triple loop with i + j - j formula seek time k is
determined If you look at the j l or less would break out

[Derived] formula

(Because the number of steps walked two are the same,
so the first time the number of steps equal to i + j
so long as we know k, l can be calculated
with i + j - k can be obtained)

【final conclusion】

Last not output f [n] [m] [ n] [m]
as a previous process at the same point will not come
as able to get f [n] [m] [ n] [m] at this point, If the output of this output is necessarily that 0
it should reach the output (n, m) that the two points
is (n - 1, m) and (n, m - 1)
is converted to an array in which f is
f [ n - 1] [m] [ n] [m - 1]
, but also because of the foregoing enumeration l j is greater than
it should be (n, m - 1) first, (n - 1, m) in the
otherwise Unable to meet l> j, 0 then the final output is
the output f [n] [m - 1 ] [n - 1] [m]

[Complete code]

#include<iostream>
#include<cstdio>

using namespace std;
const int Max = 55;
int a[Max][Max];
int f[Max][Max][Max][Max];

int main()
{
    int n,m;
    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 = 1;i <= n;++ i)
    {
        for(int j = 1;j <= m;++ j)
        {
            for(int k = 1;k <= n;++ k)
            {
                int l = i + j - k;
                if(l <= j)
                    break;
                f[i][j][k][l] = max(max(f[i][j - 1][k - 1][l],f[i][j - 1][k][l - 1]),max(f[i - 1][j][k - 1][l],f[i - 1][j][k][l - 1]));
                f[i][j][k][l] += a[i][j] + a[k][l];
            }
        }
    }
    cout << f[n][m - 1][n - 1][m]  << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11621484.html