N queen problem (bit operation implementation)

Source: Internet
Author: User

For more information, see the bit operations related to matrix67.

This article lists matrix67 bit operations and their usage skills (1) (2) (3) (4). It is worth reading.

 

This section describes the n queen problem and provides the C ++ bit operation implementation version and annotation analysis.

The Queen's problem is very classic, so we will not repeat the problem itself to solve the Queen's problem. Generally, we use the deep search DFS + Backtracking Method, according to the row (column) in the order of listing each situation that can be placed, and then determine the conflict. If the current placement is legal, search for the next layer is legal. If the placement is illegal, search for the next layer is traced back. Until a valid solution is found, each layer has a queen and there is no conflict. At this time, the number of placed solutions is 1.

Bitwise operations also use deep search and backtracking methods, but the advantage is that bitwise operations are used for conflict detection, which makes the code concise and efficient and accelerates the running speed.

The following two figures are from matrix67 (3 ):

Depth indicates the layer to be searched. The binary value of row indicates that the binary value of the current layer is 1, which indicates a conflict column. LD indicates a diagonal conflict from the upper right corner to the lower left corner, rd indicates the conflict from the upper left corner to the lower right corner.

The specific implementation code is as follows:

# Include <iostream> # include <cstdio> # include <ctime> using namespace STD; const int n = 16; // solve n queen, N cannot exceed int's bitsint upper_limit = (1 <n)-1; // 111... 1111 n bitsint ans = 0; void test (INT row, int lD, int rd) {// If the row queen is not full, if (row! = Upper_limit) {// (row | LD | rd) indicates the row, top right to bottom left lD, and top left to bottom right RD diagonal line. If it is 1, it cannot be placed ~ It indicates the position that can be placed. // because the int 32-Bit Memory is used, the N-bit upward high must be reset to 0 //, so the operation can be performed with upper_limit, extract all the positions where the queen can be placed. Int Pos = upper_limit &~ (Row | LD | rd); // all the positions in the while loop enumeration have 1, and then place the queen while (Pos! = 0) {// like the lowbit of the tree array, extract the value of the position of the last 1 of the POs binary. // It can also be written as X & (x ^ (x-1 )) int P = POS & (-Pos); Pos = POS ^ P; // set the position where the binary value of P is 1 to 0 in the POs // row | P puts the position where the binary value of P is 1 to the Queen // (LD | P) <1 update the location that cannot be placed on the next layer of the LD // (RD | P)> 1. Update the location that cannot be placed on the next layer of the RD test (row | P, (LD | P) <1, (RD | P)> 1) ;}} else ans ++;} int main () {ans = 0; clock_t start, finish; double duration; Start = clock (); test (0, 0, 0); finish = clock (); duration = (double) (finish-Start)/clocks_per_sec; printf ("time is % lf \ n", duration); printf ("Total number of cases: % d \ n", ANS); Return 0 ;}

Analysis:

Bit operation versions are indeed great in coding and time efficiency.

The key part of the code is to process the bitwise changes during row, LD, and RD searches.

When the processing value exceeds the int value, the integer type must be discarded and the bitset or bit structure must be used for related operations.

 

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.