Dynamic Programming algorithm--c++ implementation of classic cases--primary __web

Source: Internet
Author: User

Simple to complex, from easy to difficult, step-by-step. The close relatives Force Pro, writes the code.


Detailed knowledge points for dynamic programming refer to: http://blog.csdn.net/misayaaaaa/article/details/71794620


The difficulty of the dynamic programming algorithm is to abstract the dynamic programming table from the actual problem DP,DP is generally an array, may be one-dimensional or two-dimensional, or perhaps other data structures: The entire solution process can be described with an optimal decision table, the optimal decision table can be a two-dimensional table, where the row represents the stage of decision-making, The column represents the problem state, and the data that the form needs to fill out generally corresponds to the optimal value (such as the shortest path) of a certain state at a certain stage of the problem. The longest common subsequence, the maximum value, etc., the process of filling out is based on the recursive relationship, from 1 rows of 1 columns, in order to row or column priority, sequentially fill in the form, Finally, according to the data of the whole table, the optimal solution of the problem is obtained by simple choice or operation:

F (n,m) =max{f (N-1,m), F (n-1,m-w[n)) +p (N,m)}


The simplest step problem : There are N-level steps, a person at each level or level two, ask how many kinds of steps to go through the N-step method.


From the analysis that: N-Step steps, may be from the n-1 or n-2 step up, step N stage is dependent on the n-1 and n-2 of the sub-stage, so the state transition equation for dp[n] = dp[n-1] + dp[n-2], belong to the simplest dynamic programming problem

C + + implementation:

#include <iostream>

#define N        //Step number is
using namespace std;

int dp[n];          Global array, which holds the decision table

int fun (int n)      ///return steps
{
	if (n = = 1 | | n = = 2)
	{returns
		n;
	}
	Dp[n-1] = fun (n-1);        If not 1 or 2 then the recursive computation
	dp[n-2] = fun (n-2);
	Dp[n] = dp[n-1]+dp[n-2];   State transition equation return
	dp[n];
}

int main (int argc,char** argv)
{
	fun (N);
	cout<<dp[15]<<endl;        Output 15-Step
	System ("pause");
	return 0;
}


Shortest Path Problem : Given a matrix M, starting from the top left corner can only go right or down, and finally reach the lower right corner, all the numbers in the path add up to the path and, return all the path of the minimum path and, if given m below, then path 1, 3,1,0,6,1,0 is the minimum path and returns 12.


1 3 5 9
8 1 3 4
5 0 6 1
8 8 4 0

The analysis shows that when you go to the number (I, j), it is possible to come from (I-1, J) or (I, j-1), the phase of the path (I, j) is dependent on the sub stage (I-1, J) and (I, j-1), so the state transition equation is dp[i][j] =a[i][j] + min (dp[ i-1][j]+ Dp[i][j-1]), is a simple dynamic programming problem

C + + implementation:

#include <iostream>
#include <algorithm>
using namespace std;

int dp[4][4] = {};     Global array, holding the decision table

int main (int argc,char** argv)
{
	int a[4][4] = {1,3,5,9,8,1,3,4,5,0,6,1,8,8,4,0};  Matrix Storage A[i][j] for
	(int i = 0;i < 4;++i)
	{for
		(int j = 0;j < 4;++j)
		{
			if (i==0 && j== 0)                         //Boundary condition issues need to take into account
			{
				dp[i][j] = a[i][j];
			}
			else if (i==0 && j!=0)
			{
				dp[i][j] = A[i][j] + dp[i][j-1];
			}
			else if (i!=0 && j==0)
			{
				dp[i][j] = A[i][j] + dp[i-1][j];
			}
			else
			{
				Dp[i][j] = a[i][j] + min (dp[i-1][j],dp[i][j-1]);

	}} cout<< "Go to position" << "(4,4)" << "Shortest path is:";
	cout<<dp[3][3]<<endl;           Seems to be here again brain residual, true output DP[4][4] ~

	System ("pause");
	return 0;
}




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.