(1) Problem Description
A circle around the playground num placed stones piled. The stone must first have the order to merge into a pile. Provisions can only choose two adjacent pile into a new pile of stones, a pile of stones and a few new record for the merger of consuming effort. Try to design an algorithm to calculate the n heap into a pile of stones merge a number of least effort.
(2) algorithm ideas
For a given n stones piled, when only a pile when not move, and thus not consuming effort, and sequentially computing the optimal solution from the stack 2 ~ num gravel heap, stack number is incremented and the optimal solution, depending on the the resulting solution of step calculation performed;
(3) algorithm ideas
This solution matrix continually multiply and similar, we know that the matrix continually multiply two matrices is also adjacent to each merger, the stone can be consolidated matrix continually multiply way to solve.
Set DP [i] [j] denotes the i-th to j-th stack stones combined optimum value, sum [i] [j] represents the total strength of the i-th to j-consuming heap of stones. Dynamic regulation equation is as follows:
(4) Code Display
public class StoneMerge { / ** * The number of stones recording stack * / Private static int NUM; / ** * weight of each recording stack gravel * / Private static int [] weight; / ** * stones recording stack off position [easy to calculate the local optimal solution] * / Private static int [] [] LOCATION; / ** * stones recording stack local optimal solution, so that the optimal solution to obtain a final movable Regulation [equation] * / Private static int [ ] [] DP; / ** * initialization data * / Private static void initData () { Scanner INPUT = new newScanner (the System.in); System.out.println ( "Please enter the number of stones stack:" ); NUM = input.nextInt (); weight = new new int [NUM]; System.out.println ( "Please enter each stack by weight stones: " ); for ( int I = 0; I <weight.length; I ++ ) { weight [I] = input.nextInt (); } // define a two-dimensional array of type int, each finished creating direct element is initialized to 0 DP = new new int [NUM] [NUM]; LOCATION = new new int [NUM] [NUM]; } / ** * Calculated value of least most laborious * / Private static void dpFindMinStrength () { // Initialization dp array for ( int m = 0; m <NUM; m ++ ) { dp [m] [m] = 0; // pile stones, do not move, effort is spent 0 } for ( int R & lt = 2; R & lt <= num; R & lt ++) { // from 2 to sequentially stack num stack, calculates the optimum value for ( int I = 0; I <num -. 1 + R & lt; I ++) { // initial stack stones ranging int J = I R & lt + -. 1; //The r and each selected starting stack gravel stones stack i, stones stack terminates calculation int SUM = 0 ; for ( int X = i; X <= j; X ++) { // calculated from gravel stones heap stack i to j merged , the strength of the sum of the last two stacks using sUM sUM + = weight [X]; } // the dynamic regulation equation, the optimal solution from the current local stack stones from i to j stones stack strength combined sum of the used dp [i] [J] DP = [i +. 1] [J] + sUM; // calculating i stack is separated from the stone, the total effort used LOCATION [i] [J] = i; // tag stones from the i stack apart position for ( int K = I +. 1; K <J; K ++) { //Statistical gravel is separated from the stack needs k [k ∈ (i, j)] using the sum of strength int TEMP DP = [I] [k] + DP [k +. 1] [J] + SUM; // calculated from k gravel when separated from the stack, using the sum of strength IF (TEMP < DP [I] [J]) { DP [I] [J] = TEMP; LOCATION [I] [J] = K; } } } } } / ** * output * / Private static void Print () { System.out.println ( "array [number of different dynamic regulation combined stack is laborious stones]:" ); for ( int I = 0; I <NUM; I ++) { For ( int J = 0; J <NUM; J ++ ) { of System.out.print (DP [I] [J] + "" ); } System.out.println (); } System.out.println ( "The combined stack of least a number of different stones off position optimum strength:" ); for ( int I = 0; I <NUM; I ++ ) { for ( int J = 0; J <NUM; J ++ ) { the System.out .print (LOCATION [I] [J] + "" ); } System.out.println (); } } public static void main (String [] args) { // initialization data initData (); // calculate the value of least most laborious dpFindMinStrength (); // output Print (); } }
(5) Input Output
Enter stones Number of stacks: 4 Please enter the weight of each pile of stones: 4459 movable Regulation array [distinct heaps merge stones are laborious]: 0. 8 21 is 43 is 0 0. 9 27 0 0 0 14 0 0 0 0 different consolidated heap of stones most optimal solution to save energy separate location from the array [subscript 0 begin to separate]: 0 0 1 2 0 0 1 2 0 0 0 2 0 0 0 0
(6) summary
Stone merger complete withdrawal of the core idea of dynamic programming to solve the sub-problem solution [sub-problem solving are not independent of each other, interdependent] solution of the original problem and then get the solution of these sub-issues in, and then get most of the problem optimal solution.