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ⅱ