"NOIP" "Codevs" "DP" 1169 Pass the note

Source: Internet
Author: User

Woo-woo This problem is too abusive heart, it took a full two days to find out the mistake, thanks to the uestc of the two-bit gold guide ...

Title Description 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.

Enter a description input Description

The first line of input has 2 integer 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.

outputs description output Description

Outputs a total 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.

sample input to sample

3 3

0 3 9

2 8 5

5 7 0

Sample output Sample outputs

34

data size & Hint

30% data satisfies: 1<=m,n<=10

100% data satisfies: 1<=m,n<=50

The idea of this problem is very simple, get I want to run two times DP, and then two times the results of DP add up, but this greedy practice is obviously wrong, in order to get the best solution, we must keep a note of the coordinates of the state are recorded. First not to consider any time, space optimization, assuming that the note is from the upper left to the lower right corner, if we use the method of memory search, we define DP[X1][Y1][X2][Y2] for two pieces of paper respectively to (X1,Y1) and (X2,y2), and then to the end of the greatest kindness to get the sum of goodness, The definition of state is so simple ...

Then we consider the state transfer, in fact, the idea is the same as the classic memory search, dp[x1][y1][x2][y2]=max{All can transfer the point set}+MAP[X1]

[Y1]+map[x2][y2]. Of course here to deal with, if x1==x2&&y1==y2, then this state is impossible to achieve, must be removed. but I made a big mistake here ... This must take into account the end of the situation, because the end of the time will be the same, if not a special sentence, the final transfer to the end of the profit will be lost ... Here's a pit of my day ... Remember on the heartache ...

The rest of the place has nothing to say, the code is as follows:

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 Const intmaxn=Wuyi;7 intMAP[MAXN][MAXN];8 intDP[MAXN][MAXN][MAXN][MAXN];9 intd[2][2]={{0,1},{1,0}};Ten intn,m; One voidRead () A { -Cin>>m>>N; -      for(intI=1; i<=m;i++) the     { -          for(intj=1; j<=n;j++) -         { -scanf"%d",&map[j][i]); +         } -     } + } A BOOLCheckintX1,intY1,intX2,inty2) at { -     if(X1>n | | x1<1|| X2>n | | x2<1|| Y1>m | | y1<1|| Y2>m | | y2<1)return false; -     if(x1==x2 && y1==y2 &&!) (X1==n && y1==m))return false; -     return true; - } - intDfsintX1,intY1,intX2,inty2) in { -     if(Dp[x1][y1][x2][y2])returnDp[x1][y1][x2][y2]; to      for(intI=0; i<=1; i++) +     { -         intxx1=x1+d[i][0]; the         intxy1=y1+d[i][1]; *          for(intj=0; j<=1; j + +) $         {Panax Notoginseng             intxx2=x2+d[j][0]; -             intxy2=y2+d[j][1]; the             if(check (xx1,xy1,xx2,xy2)) +             { ADp[x1][y1][x2][y2]=max (Dp[x1][y1][x2][y2],dfs (xx1,xy1,xx2,xy2) +map[x1][y1]+Map[x2][y2]); the             } +         } -     } $     returnDp[x1][y1][x2][y2]; $ } - intMain () - { the read (); -Cout<<dfs (1,1,1,1);Wuyi     return 0; the}

I see a lot of the key in the online state transfer is not judged whether the focus, but all transfer, and add this sentence:

if (nx1==nx2 && ny1==ny2) dp[x1][y1][x2][y2]-=map[x1][x2].

What the hell is this? Who can explain to me ...

"NOIP" "Codevs" "DP" 1169 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.