DP --- matrix concatenation

Source: Internet
Author: User

Dynamic Programming is a way to solve the problem. It does not specify how to divide the problem into a fixed method of sub-problem to obtain the result. Instead, it divides the problem into sub-questions based on the specific instances of different input, and then computes and answers the problem.
 
The main idea of matrix concatenation is as follows:
1) set the size to the number of squares
2) The Di and j (I <j) elements at the top of the main diagonal line represent the minimum workload for Matrix Mi concatenation to Mj.
3) The following element Di, j (I> j) records to obtain the serial number of the last matrix of the first group of the smallest workload matrix group.
Finally, we can see the grouping method of the final result through the elements below.
 
Algorithm Description:
1) read n, that is, the number of matrices;
2) read n + 1 numbers, that is, the number of rows and columns of n matrices, and store them into array r;
3) set the primary diagonal element of d to zero;
4) Calculate the values of other elements in the d matrix;
5) combine n matrices.
 
Code:

[Html]
# Define MAXINT 1000
Int D [10] [10], R [11];
Void print (int I, int j)
{
Int k;
If (I = j) printf ("M % d", I );
Else if (I + 1 = j) printf ("M % d, M % d", I, j );
Else
{
K = D [j] [I];
Printf ("(");
Print (I, k );
Print (k + 1, j );
Printf (")");
}
}
 
DM (int N)
{
Int I, J, K, T;
For (I = 1; I <N-1; I ++)
For (J = 1; J <N-1; J ++)
{
D [J] [J + 1] = MAXINT;
For (K = 0; K <I-1; K ++)
{
T = D [J] [J + K] + D [J + K + 1] [J + 1] + R [J-1] * R [J + K] * R [J + 1];
If (T <D [J] [J + 1])
{
D [J] [J + 1] = T;
D [J] [J + 1] = J + K;
}
}
}
}
 
Main ()
{
Int flag = 1, N, I;
Char c;
Printf ("e ------ exit I -------- continue \ n ");
While (flag = 1)
{
Scanf ("% c", & c );
Switch (c)
{
Case 'I': flag = 1;
Printf ("Please Input The Data: \ n ");
Printf ("The value of matrix: N = ");
Scanf ("% d", & N );
For (I = 0; I <N; I ++)
{
Printf ("R [I] =", I );
Scanf ("% d", & R [I]);
} Www.2cto.com
For (I = 1; I <N; I ++) D [I] [I] = 0;
DM (N );
Print (1, N + 1 );
Break;
Case 'E': flag = 0; break;
}
}
}


Author: yyf572132811

Related Article

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.