leetcode#52 N Queensⅱ

Source: Internet
Author: User

N The Queen's question is how to place N Queens on a chessboard of nxn , and make the Queen incapable of attacking each other.

A solution to the 8 queen problem.

Given an integer n, returns the number of different solutions for the n Queens.

Example:

Input: 4 output: 2 Explanation: 4 The Queen problem has the following two different solutions. [ [". Q.. ",//solution 1" ... Q "," Q ... ",".. Q. "], ["]. Q. ",//Solution 2" Q ... "," ... Q ",". Q.. "]

classSolution { Public:    intTotalnqueens (intN) {returnQueen0,0,0,0,0, N); }    //L,r,d is the control range of the left and right vertical line    intQueenintAnswerintDeepintLintDintRintN) {if(Deep = = N)//deep current Depth = = Queen number, we fill the Queen by line, so deeper actually also represents the number of rows 0-7.8 description has filled n Queen            returnAnswer +1;  for(inti =0; I < n; ++i)//then the current deep into the Queen in turn        {                       if(((1<< i) & L) | | ((1<< i) &d) | | ((1<< i) &R))Continue; Answer= Queen (answer, Deep +1, (L <<1) | (1<< (i +1)), D | (1<< i), (R >>1) | (1<< (I-1) ), n); }        returnanswer; }};

leetcode#52 N Queensⅱ

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.