State compression for Dynamic Planning

Source: Internet
Author: User

State compression for Dynamic Planning

A question recently made on hihocoder is the original question and the problem-solving process.

Time Limit: milliseconds ms single-point time limit: 256 Ms memory limit: MB
Description

After a great deal of hardships, Xiao Hi and Xiao Ho finally arrived at the city where they held the food festival! Despite the sea of people, Xiao Hi and Xiao Ho still couldn't restrain their excitement. They put their luggage down and joined the food festival. There are a lot of interesting games on the stalls of the Food Festival. One of them is like this:

The Little Hi and the little Ho receive a rectangle plate N * M in size, and they can use this box to hold some cake of 2*1. But according to their requirements, they must fill the plate with full space and leave no gaps to take these cakes away.

Such a simple problem naturally makes it difficult for Xiao Hi and Xiao Ho, so they quickly took the cake and left ~

But Mr. Ho is not just satisfied with this, so he raised a question-how many solutions do they have to fill the N * M plate?

It is worth noting that there is a difference between the upper and lower sides of this rectangular plate, such as when N = 4, M = 3, the two solutions below are considered different!


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KCjxoNCBjbGFzcz0 = "modal-title" id = "myModalLabel"> tip: Let's play the puzzle! However, different enumeration methods may lead to different results!

Mr. Ho's observation has always been good. No, he soon discovered that the M value range may be 3, 4, or 5, however, this discovery has not been able to relieve him of much trouble.

Although in the past training period, Mr. Ho soon realized that the problem may still need to be solved using dynamic planning, but he did not think of a good way to define the State: "If I enumerate the placement of a piece of cake each time, I need to store the placement of the entire plate-that is, 2 (N * M) power state, no, no!"

Xiao Hi looked at Xiao Ho and knew that he was wrong. So he said, "Don't worry, let's look at this problem from the beginning. First, let me know first, what are our problems?"

Mr. Ho shook his head and thought, "How many solutions can be used to fill an N * M rectangle with a square of 1*2 ?"

Xiao Hi smiled and said, "Yes, we may call this answer sum (N, M). Is it possible to resolve this question into several sub-Problems?"

Mr. Ho said, "Yes, I used to think about this. I first enumerated the placement of a piece of cake, so it is a sub-problem to fill up the remaining idle positions! Because the number of such positions is decreasing, I can solve the answers to all these questions in order, but there are too many such questions, it cannot be computed at all."

Xiao Hi said, "It's too easy to think about. Is it really just a problem? You can also see that although the locations of the first enumeration are different, as long as the second enumeration is the first enumeration in another case, it will eventually reach the same state, so will repeated statistics appear next?"

Ho was surprised to say, "Yes! That ...... What should I do ?"

Xiao Hi smiled and said, "I have a plan to solve the above two problems !"

Xiao Ho said, "Come on !"

Xiao Hi waved his hand and said, "You see! For such a scheme, you can refer to the previous method, whether in the order of 1234, or in the order of 4321, and finally calculate this scheme, that is, whether it is 1 ~ Which sort of 4 would be like this, so is it a description? Every solution is counted (N * M )! Times ?"

Mr. Ho nodded and said, "Yes !" I thought about it and said, "Do I have to divide the final answer by (N * M )! What is the correct answer ?"

Xiao Hi looked helpless and said, "It's really a bad guy. Why don't you just try to make every solution count only once ?"

Small Ho: "How to do it ?! Teach me ~"

Hi, he said, "You think, since no matter what sort order will lead to the same result, we can find a way to define an order for it? For example, for each piece of cake, the row number on the left (top) side of the cake is the first keyword, and the column number is the second keyword. it is legal to sort it in ascending order !"

Mr. Ho bowed his head, and said, "Yes! In this way, the Order 1234 is valid. Similarly, each of the other schemes has only one corresponding scheme. But what I still don't understand is, how can I ensure that I can only search for such a solution when solving this subproblem? "

Little Hi said: "This is simple! As long as it is not enumerative where each piece of cake is placed? In the order that the row number is the first keyword and the column number is the second keyword, how to place the cake at each position is enumerated in sequence! In this way, we can break down a problem into sub-problems! For example, in the following question, what I want to determine now is the cake placement scheme of the black box. Different placement schemes-horizontal or vertical, different subproblems will be exported, in addition, this method does not repeat statistics as in the previous method."

Little Ho said: "It turned out to be like this! If you think about it, you will find that the exported scheme must comply with our previous requirements and is legal! But I don't mean to solve the two problems. Now I still want to record the board. Otherwise, when this happens, I cannot know whether the current position can be placed in a vertical position, and whether it has been placed in the previous placement!"

Little Hi said, "Don't worry, don't worry. You can think about how many things you want to record ~"

"Well ...... In fact, we only need to record two rows! This is because when enumeration reaches the position (I, j), that is, the row where the current enumeration is located and the next row. Because the previous positions must have been filled with cakes, and the subsequent positions must have not been filled with anything !" Little Ho suddenly realized.

"How are you going to define the status ~"

"Like this, I set sum (I, j, p1, p2 ,..., pM, q1, q2 ,..., qM) indicates that all the plates from the first row to the I-1 row have been filled up and the cake is being placed at (I, j, the placement of row I is p1... pM indicates that the placement of row I + 1 is q1... qM indicates that -- 0 indicates that it is null, and 1 indicates that the cake is placed. So sum (I, j, p1, p2 ,..., pM, q1, q2 ,..., qM) indicates how many fill methods are available for the remaining lattice in this case, and the problem we require is sum (1, 1, 0, 0, 0 ,..., 0) Now!"

Xiao Hi smiled and said, "Ah, you have noticed that sum is not the best ~ This is not just like the previous dynamic planning, it is to find a global optimal solution, but simply count the number of solutions !"

Little Ho patted his chest and said, "You don't want to see who I am! And I think the transfer will be like this ...... The first and second cases are used when the current position is not a blank position. The third and sixth cases represent the two possibilities for placing the current position of the cake ."

View big chart

Little Hi nodded and said, "That's right! Do you think there is still no way to transfer the status definition based on M ?"

Ho was surprised: "Yes! Right! We can also use the "State compression" method described in a week to compress p1... pM, q1... the 2 m 0/1 of qM represents an integer, so that my state transition equation will become like this! Si indicates the value of the I-th digit from high to low after s is converted to binary ~"

View big chart

"Yes !" Little Hi nodded. "hurry up and write a program to calculate it. Then we can eat the cake !"

For the original question, see http://hihocoder.com/contest/hiho9/problem/1click to open the link.

Problem solving process:

The questions on hihocoder are suitable for beginners, because he has a detailed explanation of the problem-solving ideas. Although I read the tips, I can basically understand how her algorithm works. But how to implement it is still difficult. So I tried the recursive method first. The Code is as follows:

# Include
  
   
# Include
   
    
# Include
    
     
Int mis (int j, int m, int st) {if (st> J-1) & 1) return 1;} int rightbank (int j, int m, int st1) {if (j = m) return 0; if (! (St1> j) & 1) return 1; else return 0;} long sum (int I, int j, int n, int m, int st1, int st2) {// printf ("% d \ n", I, j, n, m, st1, st2 ); if (I = n + 1) {if (! St1) {// printf ("11 \ n"); return 1 ;}else {// printf ("00 \ n"); return 0 ;}} if (j> m) {// printf ("> j \ n"); return sum (I + 1000000007, n, m, st2, 0) % ;} if (mis (j, m, st1) {// printf ("mis \ n"); return sum (I, j + 1, n, m, st1, st2) % 1000000007;} else if (rightbank (j, m, st1) {// printf ("ri \ n"); return sum (I, j + 2, n, m, st1 + (1 <(J-1) + (1 <
     
      
The idea of the code is very simple. According to the test, the Code also completes the task when the input is small (n is less than 14, but it is too small .)

      
Then I took a look at the several transfer equations given in the prompt, thoroughly understood the meaning of each transfer equation, and wrote the following code through the four-dimensional array method:
# Include
        
         
# Include
         
          
# Include
          
           
Long f [1010] [6] [32] [32]; int main () {int n, m, I, j, st1, st2, pj, qj, pj1; memset (f, 0, sizeof (f); scanf ("% d", & n, & m ); f [n + 1] [1] [0] [0] = 1; for (I = n; I> 0; I --) for (j = m; j> 0; j --) for (st1 = (1 <
           
            
-1; st1 --) for (st2 = (1 <
            
             
-1; st2 --) {pj = (st1> (J-1) & 1; pj1 = (st1> j) & 1; qj = (st2> (J-1) & 1; if (pj = 1 & j = m) f [I] [j] [st1] [st2] = f [I + 1] [1] [st2] [0]; else if (pj = 1 & j
             
              


              

At this time, the code can fully meet the time and space requirements of the question. But it is not good enough. The occupied space reaches 49 MB. Although the question does not meet this requirement, there is actually a lot of room for improvement, because each iteration only requires the result of the previous calculation, so we have the final version of the code below.

#include
               
                #include
                
                 #include
                 
                  long long f[2][6][32][32];int main(){int n, m, i,j,st1,st2,pj,qj,pj1;memset(f,0,sizeof(f));scanf("%d%d",&n,&m);f[1][1][0][0]=1;for(i=n;i>0;i--){for(j=m;j>0;j--)for(st1=(1<
                  
                   -1;st1--)for(st2=(1<
                   
                    -1;st2--){pj=(st1>>(j-1))&1;pj1=(st1>>j)&1;qj=(st2>>(j-1))&1;//if(st1
                    
                     

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.