Algorithm Exercise Series-hiho1048 State compression one (paving tiles)

Source: Internet
Author: User

Title Address: http://hihocoder.com/problemset/problem/1048

The beauty of programming is the same as the whole topic. (P269)

Topic

The test instructions of this subject is easy to understand, in a n*m In the lattice we now have two types of Bricks, 1 * 2 and the 2 * 1 , ask how many kinds of programs, can be the whole n*m the space is filled.

The simplest example is the following:


Programming the beauty of the topic:

summer of one year,located on the four floor of the Sigma Building, Microsoft Research Asia the building's patio with a largeScale of Decoration.the original floor was paved withNXMBlock Square Tile,These tiles are already damaged and aging.,need to beUpdate.when the decorators went to the store to buy new tiles,The Discovery store currently only offers rectangular tiles,now thea rectangular tile equivalent to the original two square tiles,the workers can't decide how much they should buy.,Reader Friendsplease help analyze:can be used1x2the tiles to coverNXMthe floor??

Here we analyze:

This topic class belongs to the state compression DP, for the state compression DP, in fact, the simplest understanding is to state the form of bits, we will use examples to illustrate.

If we are now paving the brick position (I,J) and assume that the previous position has been laid, in this position, our choice:

1. There is no need to lay the bricks, it may have been i-1 at the moment (j), and then consider (I, j+1)

2. Cross-paved brick, will (I, j+1) also paved, and then consider is (I, j+2).

3. Lay the Bricks upright (i,j) and (i+1,j) with an upright turn.

So we translate our choices as follows, in the position (I, j) if we choose to stick the tiles horizontally, then (i, J), (I, j+1) are filled in 1, if the vertical tiles , we will (i,j) fill in 0, will (i+1, j) fill in 1.

problem 1 : Why is it so counted, I think it should be understood this way:

1. In the horizontal tile, (i, J), (I, j+1) is 1, this value is actually the next row how to choose no effect.

2. The second one, we also chose 1, because this brick is over, and the choice of the next line remains unaffected.

3. While the first brick is vertical, this brick has an impact on the following, if (I,j) is 0, then (i+1,j) only 1 of the case to meet the conditions.

That is, when set to 1, there is no effect on the next line.

problem 2 : How to determine whether the current state is compatible with the state of the previous row

In fact we have basically given the analysis above, if we lay now (i,x) x here represents the line I, column X

1. If the value I line, J on the X-bit value is 0, then the first line of I-1, J value on the X-bit must be 1. Because it is not possible to place two vertical first in the same column adjacent to the next, if the test is satisfied (I, x+1), otherwise the direct return is incompatible.

2. If the value I line, J at the X position value is 1.

{


Then there are two possible scenarios:

1. (i-1, X) is 0, this time must be vertical laying, the next step is to detect (I, X + 1)


2. (i-1, X) is 1, if that is the case, then (i, X) must be to choose the horizontal paved, then (i,x+1) must be 1, and (i-1,x + 1) must be 1 (if 0, is the vertical paved), if not satisfied with the return of incompatible, meet the conditions on the test (i,x + 2)

Image

}

For the first line of compatibility, we want to do a special analysis, in the first line, either put 0, or put 1.

Adding the current test is the bit of X of DP (0, J), that is, line No. 0, x column

1. If x is 1, then x + 1 must also be 1, then test to X + 2

2. If x is 0, test the next X + 1 directly

Special Note: Here the judgment of the (i,x) must not be by (i,x-1) A bit across the paved brick, or directly x=x+2, you won't judge. (i,x) a bit.

problem 3 : Why can I use dynamic programming algorithms to solve this problem?

This has to go from the dynamic planning features:

(1) Optimal substructure

Use f[i][j] to indicate the number of schemes of the first row J State paving bricks, which must be equal to the sum of all the schemes of State K compatible with the state J of the I-1 line.

(2) Repeating sub-problem

F[I][J] That is, each state of the first row must be used in each state of line i-1.

problem 4 : From the point of view of state compression, this algorithm applies to the following conditions:

1. the solution needs to save some state data (a data value representing a state) , each State data is typically represented by a binary. This requires that there are only two states for each unit of the state data, such as a grid on a board, a pawn or a piece or a coin, or both. This uses 0 or one to represent each cell of the state data, and the entire state data is a binary number consisting of a string of 0 and 1.

2. The solution needs to implement the state data as a basic data type, such as int, long , etc., so-called state compression . The purpose of state compression is to reduce the space of data storage, on the other hand, it can improve the efficiency when the state contrast and the state overall processing. This requires that the number of units in the state data should not be too large, such as when using int to represent a state, the number of units in the state cannot exceed 32 (32-bit machines).

Code:

/* State compression dp****** filled floor HTTP://HIHOCODER.COM/CONTEST/HIHO9/PROBLEM/1 */#include <iostream> #include <algorithm  > #include <memory.h>using namespace std; #define NMAX 1000#define Mmax 1<<5bool testfirstline (int j, int M) Primarily used to test the compatibility of the first line {int i = 0;while (i < M) {if ((J & (1<<i)) = = 0)///Determine if the I bit of J is 0 for 1 then execute if I is 1 then its previous position is 0 if the judgment of the first J bit is 1 then one must be 0i++;else if (i = = M-1 | |! J & (1 << (i+1))) return False;else i + = 2;} return true;} BOOL Testcompatible (int statesa, int statesb, int M)//To determine the compatibility of the next row state Statea with the previous row status Stateb {int i = 0;while (i < M) {if (stat EsA & (1<<i) = = 0) {if ((Statesb & (1<<i) = = 0) {return false;} i++;} Else{if ((Statesb & (1<<i)) = = 0) I++;else if (i = = M-1 | |! ( (Statesa & (1<< (i+1))) && (Statesb & (1<< (i+1)))) {return false;} else i + = 2;}} return true;} int main () {int N, m;cin >> n >> m;if (M > N) {swap (M, n);} int allstates = 1 << m;long long f[nmax][mmax];int i,j;memset (f, 0, sizeof (f)), for (j = 0; J < Allstates; J + +) {if (Testfirstline (J, M)) {f[0][j] = 1;}} int k;for (i = 1; i < N; i++) {for (j = 0; J < Allstates; J + +) {for (k = 0; k < allstates; k++) {if (testcompatible, J,k, M)) {F[i][j] + = f[i-1][k]; F[I][J] = f[i][j]% 1000000007;}}} cout << f[n-1][allstates-1]<< Endl;return 0;} /* Test Case 2 45*/

Reference documents

1:http://blog.csdn.net/hopeztm/article/details/7841917 State Compression dynamic programming POJ2411 (beauty of programming-tile cover floor)

2:http://www.myexception.cn/program/1612510.html

Algorithm Exercise Series-hiho1048 State compression one (paving tiles)

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.