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)