Algorithm exercise series-hiho1048 state compression (floor tile)

Source: Internet
Author: User

Algorithm exercise series-hiho1048 state compression (floor tile)

 

After-school questions of the beauty of programming are also the same as the whole question. (P269)

Question

It is easy to understand the meaning of this question. In an N * M lattice, we now have two types of bricks: 1*2 and 2*1, how many solutions can be used to fill up the entire N * M space.

The simplest example is as follows:

Programming-US-China questions:

One summer, the Microsoft Asia Research Institute, located on the fourth floor of the magma building, performed a large-scale renovation of the office building patio. the original floor is paved with N × M square tiles, which are damaged and aged and need to be updated. when they went to the store to buy new ceramic tiles, they found that the store currently only supplies rectangular ceramic tiles. The current rectangular tile is equivalent to the original two square tiles, the workers may not decide how much they should buy. Readers may ask for help to analyze: Can I use 1 × 2 tiles to cover N × M floors?

The following is an analysis:

This topic category belongs to the state compression DP. The simplest understanding of the State compression DP is to express the State in the form of bits. We will use the example below to illustrate it.

Assume that we are in the brick position (I, j) and we have already laid the previous position. In this position, we choose:

 

1. No need to paving the bricks, may have been in the (I-1, j) moment has been vertically paved, and then consider (I, j + 1)

2. paving the bricks horizontally and (I, j + 1), and then consider (I, j + 2 ).

3. Build a vertical ro (I, j) AND (I + 1, j.

 

 

So we will translate our selection as follows. If we choose to stick bricks horizontally in the position (I, j), (I, j + 1) all are filled in as 1. If there is a vertical tile, we enter (I, j) as 0 and (I + 1, j) as 1.

 

Question 1: Why is this count? I think it should be understood as follows:

 

1. When the tiles are stacked horizontally, (I, j) AND (I, j + 1) are all 1. This value does not affect the selection of the next line.

 

2. We chose 1 when the second brick was erected. Because the brick was over, it still had no impact on how to choose the next line.

 

3. the first vertical brick has an impact on the following. If (I, j) is 0, then (I + 1, j) only when the value is 1 can the conditions be met.

That is, if it is set to 1, it does not affect the next row.

 

Question 2: How to determine whether the current status is compatible with the previous status?

In fact, we have basically provided the analysis above. If we are laying (I, x) x here, it indicates row I, column x

 

1. If the value of row I and the value of j on the x bit is 0, then the value of j on the I-1 line is 1. Because it is impossible to place the first two vertical columns in the adjacent positions of the same column. If I, x + 1 is used for the next test, otherwise the system returns an incompatible result.

2. If the value is in line I, the value of j at the position x is 1.

 

{

There are two possible cases:

 

1. (I-1, x) is 0, this time must be laid vertically, the next detection is (I, x + 1)

2. (I-1, x) is 1, if so, then (I, x) must be chosen to lay horizontally, then (I, x + 1) it must also be 1, and (I-1, x + 1) must be 1 (if it is 0, is the vertical spread), if not satisfied, return incompatible, test if conditions are met (I, x + 2)

Image

}

 

For the compatibility of the first line, let's make a special analysis. In the first line, either put 0 or 1.

 

Add the bit x of DP (0, j) to the test, that is, row 0th and column x.

 

1. If x is 1, then x + 1 must be 1, and then test x + 2.

 

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

Special note: the judgment here (I, x) must not be by (I, x-1) position across the brick, otherwise directly x = x + 2, i, x won't be judged.

 

Question 3: why can I use the dynamic planning algorithm to solve this problem?

In this case, we need to look for the features of dynamic planning:

(1) optimal sub-structure

F [I] [j] indicates the number of solutions for the j-state brick laying on line I, which must be equal to the sum of all solutions for State k that can be compatible with State j on the I-1 line.

(2) replay problem

Evaluate F [I] [j], that is, each state of row I must use each State of the row I-1.

 

Question 4: according to the characteristics of State compression, this algorithm applies to the following conditions:

1. The solution needs to save certain State data (representing a data value of a State). Each State data can usually be expressed in binary. This requires that each unit of the State data has only two States, for example, the grid on the board, the pawns, or the front and back sides of the coin. In this way, 0 or 1 is used to represent each unit of State data. The whole state data is a binary number consisting of 0 and 1.
 

2. The solution needs to implement State data as a basic data type, such as int and long, that is, State compression. The purpose of State compression is to reduce the storage space of data, and to improve the efficiency of state comparison and overall State processing. In this case, the number of units in the State data cannot be too large. For example, when int is used to represent a state, the number of units in the State cannot exceed 32 (32-bit machine ).

 

Code:

 

/* State compression DP ****** fill floor http://hihocoder.com/contest/hiho9/problem/1 */# include
 
  
# Include
  
   
Using namespace std; # define NMax 1000 # define MMax 1 <5 bool testFirstLine (int j, int M) // mainly used to test the compatibility of the first line {int I = 0; while (I <M) {if (j & (1 <
   
    
> N> M; if (M> N) {swap (M, N);} int allStates = 1 <M; 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 */
   
  
 

 

 

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.