Dynamic programming digital triangles (recursive, recursive, memory search)

Source: Internet
Author: User

Title Requirements:

7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5

In the above number triangle look for a path from the top to the bottom in the number triangle above, making the sum of the numbers on the path the largest. Each step on the path can only go down to the bottom left or right. Just ask for this maximum and can, do not have to give a specific path.
The number of rows in the triangle is greater than 1 is less than or equal to 100 and the number is 0-99

Input Format:
5//Triangle row count. Here's the triangle.
7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5

Problem Solving Ideas:

Holding a number triangle with a two-dimensional array

D[R][J]//Indicates the value of the first J element of line I;

Maxsum (I,J)//Represents the maximum of all values from the root to the first row of the maximum path;

Recursive thinking, in D (i,j) position, the next can walk in D (I+1,J) and D (i+1,j+1), recursive

Maxsum (i,j) =max (Maxsum (i+1,j), Maxsum (i+1,j+1)) +d[i][j];

The recursive code is as follows:

1#include"stdio.h"2 #defineMAX 1003 4 intN; 5 intD[max][max];6 7 intMain () {8     9     inti,j;Ten      Oneprintf"Input n:\n"); Ascanf"%d",&n); -      -      for(i=1; i<=n;i++) the     { -          for(j=1; j<=i;j++) -         { -scanf"%d",&d[i][j]); +         } -printf"\ n"); +     } A      atprintf"Max is%d", Maxsum (1,1)); - }  -  - intMaxsum (intIintj) - { -     if(i==N) in         returnD[i][j]; -     intX=maxsum (i+1, j); to     intY=maxsum (i+1, j+1); +     returnMax (x, y) +D[i][j]; - } the  * intMaxintMintN) { $     returnM>n?m:n;Panax Notoginseng}

Algorithm evaluation, because the use of recursive algorithm, deep traversal of each path, there is a large number of repeated operations, low efficiency, if the common n rows, time complexity O (n) =2 n-th square.

such as: D (3,2) is called repeatedly at the time of calculation

d (2,1) calculation calls--D (3,1), D (3,2)

D (2,2) calculation calls--D (3,2), D (3,3)

So we made improvements to the code.

Memory search, Memory dynamic programming program, introduce a maxsum[i][j] to store each maxsum (i,j) value, wait until need to use, memory search use. Time complexity o (n) = N (n+1)/2

The dynamic code is as follows:

1#include"stdio.h"2 #defineMAX 1003 4 intN; 5 intD[max][max];6 intMaxsum[max][max];7 8 intMain () {9     Ten     inti,j; One      Aprintf"Input n:\n"); -scanf"%d",&n); -      the      for(i=1; i<=n;i++) -     { -          for(j=1; j<=i;j++) -         { +scanf"%d",&d[i][j]); - Maxsum[i][j]=-1; +         } A     } at      -printf"Max is%d", Maxsum (1,1)); - }  -  - intMaxsum (intIintj) - { in     if(Maxsum[i][j]!=-1) -         returnMaxsum[i][j]; to     intX=maxsum (i+1, j); +     intY=maxsum (i+1, j+1); -Maxsum[i][j]=max (x, y) +D[i][j]; //The value to store to the (i,j) location.  the     returnMaxsum[i][j]; * } $ Panax Notoginseng intMaxintMintN) { -     returnM>n?m:n; the}

Recursion Method: Tomorrow Supplement

Summarize:

1. Recursive to dynamic general conversion method:

The recursive function has n parameters, it defines an n-dimensional array, the subscript of the array is the value range of the recursive function parameter, the value of the array element is the return value of the recursive function, so we can start from the boundary value, gradually fill the array, which is equivalent to the inverse process of calculating the value of the recursive function.

2. General ideas for solving problems of dynamic regulation:

  1) Divide the original problem into several sub -Questions. the sub-problem is the same as the original problem structure, except that the scale becomes smaller, and each sub-problem is resolved and the result is saved.

  2) determine the status. We tend to refer to a set of variables related to sub-problems as a "state". A "state" corresponds to one or more sub-problems, the so-called "state" under the "value" is the "state" corresponding to the sub-problem of the solution. A collection of all "states" that constitute the "state space" of the problem. The size of the state space is directly related to the time complexity of solving the problem with dynamic planning.

  3) Determine the value of some initial state (boundary State). in the triangle problem, the initial state is the bottom state, and the value of the original state is the value of the bottom edge.

  4) Determine the state transition equation. is the process of moving from one state to another.

Reprint please indicate the source. Newcomers seek attention.

Dynamic programming digital triangles (recursive, recursive, memory search)

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.