This paper uses a video on the internet
Dynamic programming (dynamically planned)
1, the problem is decomposed into a number of sub-problems to solve, and finally the answer to the child question answers to the original question (similar to the division of the Law)
2, the same sub-problem will appear more than once (with the division of the Law Difference)
3. Record the answer to the sub-question (memory search available, space change time)
Question one: How many kinds of 1*2 are there to fill 2*n's lattice with the dominoes?
Solve: The number of methods that fill the 2*n lattice with F (n), observing the last case where the domino was placed
State transfer equation: F (n) =f (n-1) +f (n-2) Initial conditions: F (1) =1,f (2) =2
Sinorhizobium fredii series (Fibonacci sequence)
Code: 1, top-down (generally recursive); 2, bottom-up (generally using cycle)
Question two: The N row into a column of the grid painted red, green, blue Three colors, and blue and green can not be adjacent to the question of how many kinds of coating method?
Solve1: Set f (n) as the number of methods to be applied N, but f (n) =???, State transfer equation I don't know how to write it.
Solve2:
Set F (n,0) to apply N, and the last is a red method number
Set F (n,1) to apply N, and the last green method number
Set F (n,2) to apply N, and the last blue method number
State transition equation:
F (n,0) =f (n-1,0) +f (n-1,1) +f (n-1,2)
F (n,1) =f (n-1,0) +f (n-1,1)
F (n,2) =f (n-1,0) +f (n-1,2)
Initial conditions: F (1,0) =f (=f) =1
Status: Use some numbers to uniquely represent a sub-problem
State transitions: How to move from another state to a state
What you should be aware of when defining a state
1, the state can not be too many? (Too many groups open, consider state compression)
2. Can I export the state transfer equation? (for example, in question 2, it is difficult to derive the transition equation for a state in the first-dimension state, so we redefine the state to be two-dimensional)
Define state and be able to export state transition equations
1. Is the complexity of time reasonable?
2. If unreasonable, can the state transfer equation be optimized? (such as problem three, and matrix power operation, etc.)
3, if not, try other methods to define the state
How to find the correct state transfer equation?
1, by experience
2. By inspiration
Question three: With 1*2 and 1*3 L-type dominoes fill 2*n lattice, there are several methods?
Define state: F (N) indicates the number of methods that fill the 2*n lattice (judging whether the state is too large)
State transitions:
Also consider the last method, the case of 1*2 is already discussed in question 1, so only the case of the L-type Domino is discussed.
·
·
·
Analysis: Because the good general condition above is symmetrical, only one Direction is considered
Finally put 1*2:f (n-1) +f (n-2)
Final release L-type: 2[f (n-3) +f (n-4) + +f (1) +f (0)]
State transition equation:
F (n) =f (n-1) +f (n-2) +2[f (n-3) +f (n-4) + +f (1) +f (0)] O (n) Time transfer
optimized f (n) =f (n-1) +f (n-2) +2[f (n-3) +f (n-4) + +f (1) +f (0)]
Method One: Use variables to record prefixes and : tmp=f (0) +f (1) + +f (n-3)
Such a state transition becomes: The time shift of F (n) =f (n-1) +f (n-2) +2*tmp O (1)
Method Two: Transformation of simple Transfer type
F (n-1) =f (n-2) +f (n-3) +2[f (n-4) +f (n-5) + +f (1) +f (0)]
F (n-1) +f (n-3) =f (n-2) +2[f (n-3) +f (n-4) + +f (1) +f (0)]
Get 2[f (n-3) +f (n-4) + +f (1) +f (0)]=f (n-1)-F (n-2) +f (n-3)
Transfer-to-time transfer of F (n) =2f (n-1) +f (n-3) O (1)
Method two: F (n,0): Number of methods covered with 2*n F (n,1): 2*n and the number of methods under one of the following: F (n,0) =f (n-1,0) +f (n-2,0) +2f (n-2,1) F (n,1) =f (n-1,0) +f (n-1,1) Initial state: F (0,0) =1,f (0,1) =0f (1,0) =1,f () =1f (2,0) =2,f (2,1) =2
Question four: Give a positive integer, now take some number from the inside, but two numbers can not be adjacent, seek out the number and the maximum value?
Method One: Define the state: F (N) takes a number from arr[1] to Arr[n] and takes the sum of arr[n] to the maximum state transfer: F (n) =max (f (n-2), F (n-3)) +arr[n] Final answer: Max (f (n), F (n-1))
Method Two: Define the state: F (n,0) takes a number from F (n) from arr[1] to Arr[n], and the sum Max F (n,1) for which no arr[n] is taken is the number from F (n) from arr[1] to Arr[n] and is taken to Arr[n] Total maximum State transfer: F (n,0) =max (f (n-1,0), F (n-1,1)) f (n,1) =f (n-1,0) +arr[n] Final answer: Max (f (n,0), F (n,1))
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
DP Summary (1)