Happy Birthday hdu5234 (three-dimensional DP)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5234

Test instructions: A little girl's birthday, her mother led her to a garden (n*m) inside, the starting coordinates at (a), each coordinate position has (a[i][j]) food, she can only go left or down, and her appetite Max K, finally reached (n, m) position, asked you in the case of satisfying test instructions, The maximum amount of food she can eat.

Analysis: Completely according to the DP template written, did not expect 1 A, Baidu a bit, did not think the great God's practice and I like ....

#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<vector>#include<algorithm>#include<map>#include<queue>#include<stack>#include<math.h>using namespacestd;#defineINF 0x3f3f3f3fConst intMAXN = the; typedefLong LongLL;intA[MAXN][MAXN],DP[MAXN][MAXN][MAXN];intMain () {intN, M, K;  while(SCANF (" %d%d%d", &n, &m, &k)! =EOF) {memset (A,0,sizeof(a));  for(intI=1; i<=n; i++)         for(intj=1; j<=m; J + +) scanf ("%d", &A[i][j]); Memset (DP,0,sizeof(DP));  for(intI=1; i<=n; i++)        {             for(intj=1; j<=m; J + +)            {                 for(intp=0; p<=k; p++)                {                   if(p<A[i][j]) dp[i][j][p]=max (dp[i-1][j][p], dp[i][j-1][p]); Else                   {                       ///in the case of taking a[i][j] food, judging the point on the left is larger than the point above                       intL=max (dp[i-1][J][P-A[I][J]], dp[i][j-1][P-A[I][J]]) +A[i][j]; ///If you do not take a[i][j] food, judging the point on the left and the top of the point which is larger                       intR=max (dp[i-1][j][p], dp[i][j-1][p]); ///maximum value in four casesdp[i][j][p]=Max (L, R); } }}} printf ("%d\n", Dp[n][m][k]); }    return 0;}/*4 4 2 3 (2 3, 2 3) 2 3 4*/
View Code

Happy Birthday hdu5234 (three-dimensional DP)

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.