This blog is mainly about the introduction of dynamic planning, that is, the idea of dynamic planning, and then explain the most simple method of dynamic planning.
First, what is dynamic planning?
Dynamic programming is to define the relationship between the state of the problem and the state by splitting the problem, so that the problem can be solved in the way of recursion (or divide and conquer). In fact, the decomposition of the problem, divide and conquer. This may be said that people do not understand, in fact, this is a bit similar to the recursive formula in mathematics. To give a simple example, look at the following question:
N-Step stairs upstairs problem: one can walk two or one order, ask how many kinds of way upstairs.
This is a simple story of dynamic programming. Get this question, everyone is not a bit crazy, how exactly do this? is not the idea, then according to the dynamic planning idea, we can first analyze the problem, each time there are two kinds of jumping method, respectively, is the first order or two orders, then if the current is the nth step, then the Jump method is (n-1) steps of the jump number plus (n-2) steps of the Jump method number? If the cost-effective formula is F (n) = f (n-1) +f (n-2). F (n) represents the number of jumps of the nth step.
This is not the equivalent of finding a recursive formula and then calculating it. The specific code is implemented as follows (non-recursive):
#include <stdio.h>intMain () {intI,n; Long Longa[ -]; while(~SCANF ("%d",&N)) {a[1]=1; a[2]=2; for(i=3; i<=n;i++) A[i]=a[i-1]+a[i-2]; printf ("%lld\n", A[n]); } return 0; }
Here is another example to help understand:
n letters put in n envelopes, asking for all misplaced, total number of methods, remember n elements of the total number of rows of f (n)
For this problem, we can use the same ideas as above, whether we should consider looking for a similar recursive formula to solve. Ideas are as follows:
In any one of the wrong-loading scenarios, assume that the n envelope is filled with the letter of the K envelope, while the letter in the n envelope is packed in the M envelope. We divide the total error mode into two categories according to the equivalence of K and M. If k is not equal to M, the letter of the n envelope and M envelope, the envelope of n is exactly the corresponding letter, and the letter "M" in the wrong pack K envelope, that is, except for the envelope of N, the rest of the n-1 envelope all wrong, the wrong way is equal to f[n-1], and because of the n-1 of M may value, The total number of such error modes is (n-1) * f[n-1]. It can also be understood that, on the basis of the n-1 of the f[n-1] method of the envelope, the letter of the n envelope is exchanged with the letter from any envelope in the n-1 envelope (selected in a total of n-1), and the number of all the envelopes are incorrectly installed. In another case, if K equals m, after the letter of the n envelope and the M envelope are exchanged, the letter N envelopes and M envelopes are exactly the corresponding letters, so that the remaining n-2 envelopes except them are all wrong, and the wrong way is f[n-2], and because of the n-1 value of M, the total number of such wrong-loading methods is (n-1) * f[n-2]. It can also be understood that, on the basis of all n-2 envelopes, the letters in the last two envelopes (n envelopes and any one of the 1 to n-1 envelopes, a total of n-1 options) are exchanged and the number of ways in which all envelopes are incorrectly loaded. In summary, f[n] = (n-1) * f[n-1] + (n-1) * f[n-2]. This is the wrong row formula.
The specific code is as follows:
#include <stdio.h>LongLongf[ +];//long long for larger valuesintMain () {f[1] =0; f[2] =1;//Initial value for(inti =3; I <= -; i + +) F[i]= (I-1) * F[i-1] + (I-1) * F[i-2];//recursion to get every number in a sequence intN; while(SCANF ("%d", &n)! =EOF) {printf ("%lld\n", F[n]);//Output}return 0;}
Dynamic programming of algorithms (recursive solution one)