Status compression DP entry question poj 1185 Artillery Position

Source: Internet
Author: User

Poj 1185 Artillery Position

Chinese questions.

Solution: You can see from the question that the status of each row is only related to the first two rows. N <100, m <= 10 is known. Obviously, m can be compressed, and the status of each position in M can be expressed as 0, 1. Therefore, the total state of M can be expressed by the decimal number converted from the binary value with the length of M. The calculation shows that there are not many legal states, because two soldiers in the same row cannot attack each other.

Key code for finding a valid status:

REP(i, (1<<m)) {        p = i;        if(((p << 1) & i) || ((p << 2) & i))    continue;        num[sum] = 0;        while(p) {num[sum] += (p&1); p >>= 1;}        status[sum++] = i;}

Status [p] = I indicates that the valid status of the nth P is I. Num [I] = A, indicating that a artillery was deployed when the status was I.

After the data is processed, the status 0 of the current checker indicates P and 1 indicates H. Chess [I] = s indicates the status of row I is S.

Set DP [I] [J] [k] to indicate that the status of row I is J and that of row I-1 is K.

DP [I] [J] [k] = max (DP [I-1] [k] [p]) + num [J];

Yes (status [J] & status [k]) = 0

(Status [J] & chess [I]) = 0 & (status [k] & chess [I-1]) = 0)

(Status [k] & status [p]) = 0 & (status [J] & status [p]) = 0)

For DP [0] [J] [K], it is provided separately during initialization. DP [0] [J] [k] = num [J]; (if it is not in conflict with the original Board)

 

PS: The pressure DP is also a God-class topic. It must be done well after the poj training program is completed! I will start to dig into poj tomorrow. Come on !!

 

 

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.