Maximum value of path elapsed (minimum):
The original question: There are n*m on the plane, a certain number of apples are placed in each lattice. From the upper left grid, each step can only go down or to the right, each time you go to a grid to collect the apples in the lattice, so go straight to the lower right corner, ask the maximum number of apples can be collected.
You might want to use a table to indicate:
{5, 8, 5, 7, 1, 8},
{1, 3, 2, 8, 7, 9},
{7, 8, 6, 6, 8, 7},
{9, 9, 8, 1, 6, 3},
{2, 4, 10, 2, 6, 2},
{5, 5, 2, 1, 8, 8},
In this 6x6 form, fill in some numbers to indicate the number of apples in the grid. According to the rules of the topic "Each step can only go down or to the right", if using X for portrait, with Y for horizontal, then can reach A[x,y] at only two positions A[x-1,y] (the last lattice) and A[x,y-1] (left a lattice), so it is necessary to take these two positions in the larger point. This goes back to a[0,0], or from a[0,0] to a[x,y].
.............., A[x-1,y]
..., a[x,y-1], A[x,y],
Based on this, we can fill in a table with the maximum number of apples that can be collected at each point in the first row and the first column from the top left corner. As follows:
Then fill in line 2nd, the first is the 2nd row 2nd column value, should be filled with MAX (a[1,2], a[2,1]) + a[2,2] corresponding to the number of apples. That is, to reach the 2nd row 2nd column can obtain the maximum number of apples, to see the 2nd row 1th column obtained by the number of apples (6) and 1th row 2nd column obtained by the number of apples (13), which is larger, who is large to take whose value, obviously 1th row 2nd column obtained by the number of apples (13) greater, So the number of apples with 13 plus the 2nd column in line 2nd is 3 = 16, which is the maximum number of apples that can be obtained by the 2nd row 2nd column. Similarly, the maximum number of apples that can be obtained by filling in the grid is to see the left one and the upper one, whichever is larger, and the maximum number of apples that can be obtained by adding the number of apples to the lattice. Fill in all the squares and get the final:
So: The maximum number of apples to reach in the lower right corner is 76. The path can be obtained by backward way, from the bottom right corner to see the left side of the lattice and the top of a lattice which way to go, if you encounter the same big, choose one.
So we can draw a road map, such as the right table:
The analysis and resolution of this example is probably the case. In the first example above, we mentioned that space change time is the essence of dynamic programming. But whether a problem can be solved with a dynamic programming algorithm depends on whether the problem can be broken down into smaller problems (sub-problems). And whether there is a relationship between sub-problems, is the difference between the dynamic programming algorithm and the division of the location of the law. In general, each sub-problem of the division of the law is independent of each other, such as binary search (binary search), merge sort, and so on. However, the sub-problems of the dynamic programming algorithm tend to encounter repeated sub-problems when they are subdivided into smaller sub-problems, we only deal with the same sub-problem once and save the result of sub-problem, which is the biggest feature of dynamic programming.
The dynamic programming algorithm is summed up as two points:
1 Looking for recursive (recursive) relationships, the more professional version is called the state transition equation.
2 Save intermediate State, space change time.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceseqlistsort{/// <summary> /// <ather> ///Lihonglin/// </ather> /// <content> ///There are n*m on the plane, and a certain number of apples are placed in each lattice. You start with the grid in the upper-left corner, and each step can only go down or///go right and collect the apples from the lattice every time you go to a grid, so go straight to the bottom right, and you can collect as many apples as you like. ///Input:///Enter the number of rows and columns in the first row///randomly generate the number of apples in each lattice///output: The maximum number of apples that can be received. /// ///thought analysis: This is a typical two-dimensional array dp problem///Basic Status: The number of apples collected dp[x,y] When you reach the grid in column x row y. ///transfer equation: Because you can only go to the right or down, so when you reach the grid in line x row y, you may be from line x-1///Column y or line x y-1 to the grid, and we end up collecting the most apples. ///so: dp[x,y] = max (if (x>0) dp[x-1,y], if (y>0) dp[x,y-1])/// </content> /// </summary> classAPPLE_DP {Static int[,] a =New int[ -, -]; Static int[,] dp =New int[ -, -]; Static intM,n; Static voidCollectapple (intXinty) {dp[x, y]=a[x, y]; intMax =0; if(X >0&& Max < dp[x-1, Y])//the end of the upper one is x, y{Max= Dp[x-1, y]; } if(Y >0&& Max < dp[x, y-1])//the end of the left one is x, y{Max= Dp[x, Y-1]; } dp[x, Y]+=Max; if(X < M-1) {collectapple (x+1, y); } if(Y < n-1) {collectapple (x, y+1); } } Public Static voidInitmap () {Console.WriteLine ("Please enter M row, n column"); M=Convert.ToInt32 (Console.ReadLine ()); N=Convert.ToInt32 (Console.ReadLine ()); Random R=NewRandom (); for(intI=0; i<m;i++) { for(intj=0; j<n;j++) { //A[i,j] = r.next (1,m + N);Console.Write ("{0,-3}", A[i, j] = R.next (1, M +N)); } Console.WriteLine (); } collectapple (0,0); //Print Results for(intI=0; i<m;i++) { for(intj=0; j<n;j++) {Console.Write ("{0,-3}", Dp[i, j]); } Console.WriteLine (); } } }}
The collection of dynamic planning apple