[Leetcode series] recursive solution to n queen problem-bit Operation Method

Source: Internet
Author: User

N queen's question:

Given the 8*8 board, place n Queens so that they cannot attack each other (that is, the two queens cannot be placed on the same row/column/positive/negative diagonal). How many placement methods are there to solve?

There are a lot of answers to this question on the Internet, but I don't see much of the bit operation solution. I will not repeat the code and illustration below.

1 class solution {2 public: 3/* uses the backtracking algorithm implemented by bit operations. scan by row to detect columns that can be placed. 4 * 'limit'-all are '1 '. it indicates that all columns are occupied by 5 * 'h', which is the vertical projection of all current Queen columns on the row. if H = limit, the search is complete. Answer ++. 6 * 'r'-it is the vertical projection of all the Queen's diagonal lines. 7 * 'l'-it is the vertical projection of the diagonal lines of all queens currently. 8 * H | r | L-all occupied places. therefore, Pos = Limit &(~ (H | r | L) is all free positions. 9 * P = POS & (-Pos) Find the rightmost 1. pos-= P indicates placing a queen on the one represented by P. 10 * 'H + p'-is the vertical projection of the new queen's columns on the row. 11 * '(R + p) <1' is the vertical projection of the new queen's diagonal. because we move to the next row, the projection is tilted from right to left, 12 * we need to move to the next row and then shift to the left position. 13 * '(L + p)> 1' is the vertical projection of the new queen's diagonal line. because we move to the next row, the projection is tilted from left to right, and 14 * We need to translate the position to the right after moving to the next row. 15 */16 int ans, limit; 17 int totalnqueens (int n) {18 ans = 0; 19 limit = (1 <n)-1; 20 DFS (0, 0, 0); 21 return ans; 22} 23 void DFS (in T h, int R, int L) {24 if (H = Limit) {25 ans ++; 26 return; 27} 28 int Pos = Limit &(~ (H | r | L); // use a bit and remove a high value of 0 to obtain all positions that can be placed in this row. 29 While (POS) {30 // because the parts with more than 8 POS bits are 0, the following analysis only targets low 8-bit 31 int P = POS & (-Pos ); // The result of obtaining a negative number is the POs rounded up to + 1, and the & Pos result is the lowest position of the POs. 1 32 pos-= P; // remove this 33 DFS (H + P, (R + p) <1, (L + p)> 1 ); // Add the new p34 directly to the column direction restriction. // For The Next row, the diagonal limit is one grid 35} 36} 37 on both sides of P };

 

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.