HDU 5234 Happy Birthday Dynamic programming (three-dimensional array)

Source: Internet
Author: User

The main topic: birthday, there is a n*m table, each location has a certain weight of cake you can choose to eat or not to eat, from (to N,m), each time can only go to the right or down, up to eat K-weight cake. Ask you how many cakes you can eat.

Topic Idea: Before the 01 backpack we are all using a one-dimensional array v[] to store, but this time to use a two-dimensional array map[i][j] to store the value of a point, the current point by map[i][j-1] or map[i-1][j] go.

State transition equation: Dp[i][j][q]=max (Dp[i][j][q],map[i][j]+max (Dp[i][j-1][q-map[i][j]],dp[i-1][j][q-map[i][j])).

#include <cstdio>#include<stdio.h>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<queue>#defineINF 0x3f3f3f3f#defineMAX 105using namespacestd;intDp[max][max][max],map[max][max];intMain () {intn,m,k,v1,v2,i,j,q;  while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) {memset (DP,0,sizeof(DP));  for(i=1; i<=n;i++)        {             for(j=1; j<=m;j++) {scanf ("%d",&Map[i][j]); }        }         for(i=1; i<=n;i++)        {             for(j=1; j<=m;j++)            {                 for(q=k;q>=map[i][j];q--) {v1=max (dp[i][j-1][q],dp[i-1][j][q]); V2=map[i][j]+max (dp[i][j-1][q-map[i][j]],dp[i-1][j][q-Map[i][j]]); DP[I][J][Q]=Max (V1,V2); }}} printf ("%d\n", Dp[n][m][k]); }    return 0;}
View Code

HDU 5234 Happy birthday Dynamic planning (three-dimensional array)

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.