The recursive equation is data [m] [n] [x] = (data [m+1] [n] [x value [m] [n]]+(data [m+1] [x value]). All the bits of the initial value are 0, and only the bottom line is 1, such as 2 in the lower left corner. You can set data[8][0][2] = 1. Understand the meaning. Then recursively. If you type the table, the complexity will not exceed 9*9*60. In fact, many values will be much faster if they are not counted.
If it's not acm…… ......................................................................................................................................................................... . .
************************************************************************************************************
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# Define M 9
# Define MaxSum 6 1
int data_value[M][M] = {
7, 0, 0, 0, 0, 0, 0, 0, 0,
4, 6, 0, 0, 0, 0, 0, 0, 0,
6, 9, 3, 0, 0, 0, 0, 0, 0,
6, 3, 7, 1, 0, 0, 0, 0, 0,
2, 5, 3, 7, 1, 0, 0, 0, 0,
5, 9, 4, 7, 3, 2, 0, 0, 0,
6, 4, 1, 8, 5, 6, 3, 0, 0,
3, 9, 7, 6, 8, 4, 1, 5, 0,
2, 5, 7, 3, 5, 7, 8, 4, 2
}; //data
int data _ DP[M][M][MaxSum]; //array of dynamic programming, [row] [column] [sum] = method number, and method number =- 1 indicates that it has not been calculated.
Void initialization ()
{
int i,j,k;
int tar
for(I = 0; I<m-1; I++) // Assign values to the first m- 1 lines.
for(j = 0; j & ltm; j++)
for(k = 0; k & ltMaxSumk++)
data _ DP[I][j][k]=- 1; //-1 indicates that it has not been calculated.
for(j = 0; j & ltm; J++) // Assign value to line m
{
tar = data _ value[M- 1][j];
for(k = 0; k & ltMaxSumk++)
{
If (k == tar) // The number of methods is 1. For example, because data_value[8][0] == 2, data_dp[8][0][2] = 1.
data _ DP[M- 1][j][k]= 1;
Else // Other is 0.
data _ DP[M- 1][j][k]= 0;
}
}
}
Int func_dp (integer row, integer column, integer value)
{//Dynamic programming function, using recursion.
if (data_dp[row][col][value]! =-1)/If calculated, return directly.
Returns data _ DP [row] [col] [value];
/* Method number = lower left corner+lower right corner */
int left = func_dp(row+ 1,col,value-data _ value[row][col]);
int right = func_dp(row+ 1,col+ 1,value-data _ value[row][col]);
Int sum = left+right;
data _ DP[row][col][value]= sum; //Because it has not been calculated, the result is saved and used directly in the next visit to avoid repeated calculation.
Returns data _ DP [row] [col] [value];
}
int main()
{
init();
Printf("row: 1, col: 1 value: 60 ");
printf("%d\n ",func_dp(0,0,60));
/* * * * * * * * Debugging * * * * * * * * *
int tmp_row,tmp_col,tmp _ value
while(scanf("%d%d%d ",& amptmp _ row & amp; tmp _ col & amp; Tmp value) == 3)
{
Printf ("row: %d, column: %d, value: %d %d\n", tmp_row, tmp_col, tmp_value, func_dp(tmp_row- 1, tmp _ col-0/,tmp _ dp).
}
System ("suspended");
/* * * * * * * * Debugging * * * * * * * * *
Returns 0;
}
****************************************************************************************************
Sampling time measurement.
Row: 1, column: 1, value: 606 (the result is 6).
1 1 60 (entered manually, the same below)
Line: 1, column: 1, value: 60 6.
2 1 53
Row:2, col: 1, value: 53 2 (for 1, 1 at the lower left is 2).
2 2 53
Row:2, col:2, value:53 4 (for 1, 1 is 4 in the lower right corner. Verification 6 = 2+4)
3 1 49
Row:3, col: 1, value:49 0 (for 2, 1, the bottom left is 0).
3 2 49
Row:3, col:2, value: 49 2 (for 2 1, the lower right is 2, and the verification is 2 = 0 +2).
3 2 47
Row: 3, column: 2, value: 47 3 (for 2, 2 is 3 in the lower left corner).
3 3 47
Row:3, col:3, value:47 1 (for 2, 2 is 1 in the lower right corner, and verification 4 = 3+ 1).
********************************************************************************************
If you don't understand, let's take a look at what dynamic programming is.