HDU 5234 Happy Birthday
Test instructions
Today is Gorwin's birthday. So her mother is going to fulfill one of her wishes. Gorwin said she wanted to eat a lot of cakes. So his mother took her to the cake garden.
The garden was divided into n*m squares. In every lattice, there is a cake. Line I, column J in the lattice has a weight of w[i][j] kilograms of cake, gorwin from the upper left corner of the lattice to walk, go to the lower right corner (n,m) lattice. In each step, Gorwin can go right or down, that is: Gorwin (i,j), she can walk to (i+1,j) or (i,j+1) (but she cannot walk out of the garden).
When Gorwin arrives at a lattice, she can eat or not eat the cake in that lattice. But she can't just eat part of it. Her stomach is not so big, so she can only eat a K-kilogram cake. Now, Gorwin is standing in the upper left corner, looking at the map of the cake garden and trying to find a way to make the most of the cake she eats. Please come and help me.
Limit:
0 < N,m,k,w[i][j] <= 100
Ideas:
01 Backpack
Deal with one dimension, two-dimensional on the line.
Complexity O (n^3)
/*hdu 5234 Happy Birthday test instructions: Today is Gorwin's birthday. So her mother is going to fulfill one of her wishes. Gorwin said she wanted to eat a lot of cakes. So his mother took her to the cake garden. The garden was divided into n*m squares. In every lattice, there is a cake. Line I, column J in the lattice has a weight of w[i][j] kilograms of cake, gorwin from the upper left corner of the lattice to walk, go to the lower right corner (n,m) lattice. In each step, Gorwin can go right or down, that is: Gorwin (i,j), she can walk to (i+1,j) or (i,j+1) (but she cannot walk out of the garden). When Gorwin arrives at a lattice, she can eat or not eat the cake in that lattice. But she can't just eat part of it. Her stomach is not so big, so she can only eat a K-kilogram cake. Now, Gorwin is standing in the upper left corner, looking at the map of the cake garden and trying to find a way to make the most of the cake she eats. Please come and help me. Limit: 0 < n,m,k,w[i][j] <= 100 ideas: 01 backpack deal with a one-dimensional, two-dimensional on the line. Complexity O (n^3) */#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int n= 105;int a[n][n];int dp[n][n][n];void init () {memset (dp,0,sizeof (DP));} void Gao (int n,int M,int v) {for (int i=a[0][0];i<=v;++i) {dp[0][0][i]=a[0][0];} for (int i=1;i<n;++i) {for (int j=0;j<=v;++j) {if (j<a[i][0]) Dp[i][0][j]=dp[i-1][0][j];elsedp[i][0][j]=max ( Dp[i-1][0][j],dp[i-1][0][j-a[i][0]]+a[i][0]);}} for (int i=1;i<m;++i) {for (int j=0;j<=v;++j) {if (J<a[0][i]) Dp[0][i][j]=dp[0][i-1][j];elsedp[0][i][j]=max ( dp[0][I-1][j],dp[0][i-1][j-a[0][i]]+a[0][i]);}} for (int i=1;i<n;++i) {for (int. j=1;j<m;++j) {for (int k=0;k<=v;++k) {if (K<a[i][j]) Dp[i][j][k]=max (dp[i-1][ J][k],dp[i][j-1][k]); Elsedp[i][j][k]=max (Max (Dp[i-1][j][k],dp[i-1][j][k-a[i][j]]+a[i][j]), Max (DP[I][J-1][K],DP I [J-1] [K-a[i][j]]+a[i][j]);}}} printf ("%d\n", Dp[n-1][m-1][v]);} int main () {int n,m,k;while (scanf ("%d%d%d", &n,&m,&k)!=eof) {init (); for (int. i=0;i<n;++i) for (int j=0;j <M;++J) scanf ("%d", &a[i][j]); Gao (n,m,k);} return 0;}
HDU 5234 Happy Birthday