HDU 2571 DP Entry question

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2571

Regular DP, just let me get this rookie to find the sense of state transfer equation.

Definition Dp[i][j] represents the largest lucky value owned by Row J of line I.

DP[I][J] is transferred from three cases: 1, from the above coordinate transfer, that is dp[i-1][j]. 2, transferred from the left, that is dp[i][j-1]. 3. Transferred from the factor column of J in his same line (except for J himself), that is, the factor column of dp[i][J.

Find the largest of these three cases, dp[i][j] + = max (...)

That's how I deal with it. First find out the largest of the series, and save it, so that it will be converted into the largest of the three.

To pay special attention to the processing of the boundary situation, because the topic said there may be negative, then the boundary situation to be a special discussion, because 0 is not the smallest, it is possible to affect the maximum value of the determination (such as all negative, the result boundary is 0, but as the largest), If it is the first line can only be transferred from the above 2, 3, if it is the first column, only from 1 This situation transfer, dp[1][1] words are itself.

See discuss feel very inspired, can also be dp[][0] dp[0][] are set to-inf, and Dp[1][0] dp[0][1] set to 0 such a boundary situation will not appear special situation, do not need special discussion.

Code + Comment:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace    STD; #define M 1009#define INF 0x3f3f3f3fint dp[m][m];int main () {int n,m,t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m); for (int i = 0;i <= n;i++)//can also be cleverly processed by the initialization next to the boundary so that the boundary condition does not require special consideration//dp[i][0] =-inf;        Set the border next to-inf so that it does not affect the result//for (int i = 0;i <= m;i++)//dp[0][i] =-inf; DP[0][1] = dp[1][0] = 0;//set to 0 for (int i = 1;i <= n;i++) next to the first point {for (int j = 1;j <= m;j+            +) {scanf ("%d", &dp[i][j]);                }} for (int i = 1;i <= n;i++) {for (int j = 1;j <= m;j++) {                int MAXN =-inf; for (int k = 1;k < j;k++) {if (j%k==0) {if ( MAXN&LT;DP[I][K]) MAXN = Dp[i][k];  }} if (I==1 && j==1) continue;  You can also use the ingenious initialization above to replace the special discussion boundary else if (i==1) dp[i][j] + = max (DP[I][J-1],MAXN);                Boundary conditions are particularly discussed because the subject can have negative numbers so 0 is not the smallest else if (j==1) dp[i][j] + = dp[i-1][j];                else Dp[i][j] + = max (Dp[i-1][j],max (DP[I][J-1],MAXN));            printf ("de--%d", Dp[i][j]);        }//printf ("\ n");    } printf ("%d\n", Dp[n][m]); } return 0;} /*13 8-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 -1*///Note processing the boundary otherwise this set of data will go wrong <span style= "color: #ff0000; " ></span>


HDU 2571 DP Entry question

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.