Poj3254 -- Fields + state compression dp

Source: Internet
Author: User

Poj3254 -- Fields + state compression dp

The first State compresses dp :)

Consider the situation of each row. If we make 0 to indicate that grazing is not allowed, 1 to indicate grazing, then all feasible situations in this line can be outputted and correspond to a decimal number; this is the state compression. Then, the question can tell whether the status of each row can appear only in relation to the row before it, therefore, we can define dp [I] [j] to indicate the number of grazing methods when row I is in the j state;

Dp [I] [j] = dp [I-1] [j1] + dp [I-1] [j2] + .... + Dp [I-1] [jn], where j and jn can appear simultaneously.

By using this equation, we can write a program. We can use an array to represent the State and make corresponding judgments. However, we can use bitwise operations to conveniently implement these judgments.

First, we do not need to put every State in question. We can filter out all valid states through the question Restriction Conditions (two 1 cannot be adjacent). This operation uses x & (x <1) if the value is 0, it indicates that there is no adjacent State; otherwise, we also need to determine whether these valid States conflict with the initial status. In this case, we need to reverse the initial status 01, 0 is changed to 0 (in this way, you only need to determine if the status corresponding to the original status 1 is 0, you can use & to implement it), so that we only need to change a status &, if it is 1, it indicates it is invalid. If it is 0, it is legal. Finally, we need to determine whether the same bit of the two numbers is 1 at the same time. This can also be achieved through.

After these operations are implemented using bitwise operations, we can easily write code.


The Code is as follows:


# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define mod limit 00000int total [1000]; int dp [15] [1000]; int top; int cur [15]; int n, m; int OK (int I, int j) {if (I & j) return 0; return 1 ;}void Allstate () {top = 1; int j = 1 <
    
     


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.