An introduction to algorithmic learning-memory search for dynamic programming

Source: Internet
Author: User

I. Dynamic planning

Dynamic programming (programming), which is similar to "divide-and-conquer thought", is used to divide the problem into sub-problems, and to obtain the solution of the whole problem by merging the consider of sub-problems. The difference between "divide and conquer" is that the dynamic programming algorithm for a same sub-problem does not compute the second time, and its implementation principle is to save the value of each computed sub-problem in a table.

Two. Memory Search

Our common dynamic programming problems, such as pipeline scheduling problem, matrix chain multiplication problem and so on are "step by step", that is, the problem of scale I need to base on the problem of scale i-1 optimal solution selection, the usual recursive mode is DP (i) =OPTIMAL{DP (i-1)}. While the memory of the search is also the DP idea, when sub-problem A and sub-problem B has sub-problem C, if sub-sub-problem C is the optimal solution has been found, then sub-question A or B only need to "look up the table" to obtain the solution of C, and do not need to calculate the C again. The DP mode of the memory search is "random" than the normal mode, usually DP (i) =optimal (DP (j)), J < I.

Three. Skiing problems

The snow field shown as R*c, R is the number of rows, and C is the number of columns. The number in the circle represents the height of the snow field H, according to common sense we know that skiing can only slide from the top down to move, now we want to be able to glide the longest distance, the above example we can directly determine the 25-24-......- 1 This clockwise spiral ski mode can slide the farthest.

So how does this problem be programmed to be implemented? We found that this is a typical recursive, DP (i, j) to indicate the maximum length that can be slid from the coordinates (I,J), and there are: DP (i, J) =optimal{dp (i±1, j±1)}+1. Paste the source code below.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intMax_size= the;7 intr,c;8 intdir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};9 intH[max_size][max_size],dp[max_size][max_size];Ten intInmap (intXinty) { One     if(x>=0&&x<=r-1&&y>=0&&y<=c-1)return 1; A     return 0; - } - intMAX2 (intAintBintCintd) { the     returnMax ( A, b), Max (c,d)); - } - intDfsintIintj) { -     intnx,ny,down=0, up=0, left=0, right=0; +     if(Dp[i][j])returnDp[i][j]; -nx=i+dir[0][0]; ny=j+dir[0][1]; +     if(Inmap (Nx,ny)) { A         if(H[i][j]>h[nx][ny]) up=DFS (nx,ny); at     } -nx=i+dir[1][0]; ny=j+dir[1][1]; -     if(Inmap (Nx,ny)) { -         if(H[i][j]>h[nx][ny]) right=DFS (nx,ny); -     } -nx=i+dir[2][0]; ny=j+dir[2][1]; in     if(Inmap (Nx,ny)) { -         if(H[i][j]>h[nx][ny]) down=DFS (nx,ny); to     } +nx=i+dir[3][0]; ny=j+dir[3][1]; -     if(Inmap (Nx,ny)) { the         if(H[i][j]>h[nx][ny]) left=DFS (nx,ny); *     } $DP[I][J]=MAX2 (up,down,left,right) +1;Panax Notoginseng     returnDp[i][j]; - } the intMain () { +scanf"%d%d",&r,&C); Amemset (H,0,sizeof(h)); theMemset (DP,0,sizeof(DP)); +      for(intI=0; i<r;i++){ -          for(intj=0; j<c;j++){ $scanf"%d",&h[i][j]); $         } -     } -     intans=-1; the      for(intI=0; i<r;i++){ -          for(intj=0; j<c;j++){Wuyians=Max (Ans,dfs (i,j)); the         } -     } Wuprintf"%d\n", ans); -}

Four. The problem of cutting sticks

Give you a long n -foot stick and a price list for the bar as follows (where i =,..., n), how to sell the stick to the highest price, you can cut the stick.

The problem can also be achieved using DP memory search, with the recursive formula DP (n) =optimal{max{price (i) +DP (n-i) |1≤i≤n}}. The implementation code is as follows:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMax_size= -;6 Const intinf=1<< -;7 intPrice[max_size],dp[max_size];8 intN;9 intDfsintN) {Ten     if(Dp[n])returnDp[n]; One     if(n==0)return 0; A     intmmax=-inf; -      for(intI=1; i<=n;i++){ -Mmax=max (Mmax,price[i]+dfs (ni)); the     } -dp[n]=Mmax; -     returnDp[n]; - } + intMain () { -      while(SCANF ("%d", &n)! =EOF) { +          for(intI=1; i<=n;i++) scanf ("%d",&price[i]); Aprintf"%d\n", DFS (n)); at     } -}

Five. Summary

Through the first two examples of analysis, we can draw the DP Memory search algorithm template (DIY, you can choose to reference)

1 Dfs (Problem a) {2     if(A has been solved)3 Then:consult the record.4     Else//get the optimal solution of problem a.5Divide the problem a into several sub-problems (A1,A2,..., AK)6         Getthe solution of problem A by DFS (A1), DFS (A2),..., DFS (AK).7     finallywrite the optimal solution into record.8}

An introduction to algorithmic learning-memory search for dynamic programming

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.