For each case of N-queens, the number of permutations of n-queens for each case is answered.
L,r and C are the pieces of each type of lattice.
L judge that the diagonal lattice R is judging by the diagonal lattice C judging is such a vertical bar lattice
Enumerate each line to put a piece of chess, judging whether conflict with the previous, if not conflict always let answer +1
1 classSolution {2 Public:3std::vector<BOOL>l,r,c;4 intans;5 intTotalnqueens (intN) {6L.resize (2* n +1,false);7R.resize (2* n +1,false);8C.resize (N,false);9Ans =0;TenN_queens (0, n); One l.clear (); A r.clear (); - c.clear (); - returnans; the } - voidN_queens (intNowintN) {//Calculate the possible number of N_queens - if(now = =N) {//recursion to the end -ans++; + return; - } + Else{ A for(inti =0; I < n; ++i) { at if(Is_legal (now, I, N)) {//not in conflict with before put a pawn on it. -Init (now, I, N,true); -N_queens (now +1, n); -Init (now, I, N,false); - } - } in } - } to BOOLIs_legal (intXintYintN) {//Determine if conflict with previous + return!c[y] &&!l[x-y + n] &&!r[x +y]; - } the voidInitintXintYintNBOOLb) {//l,r and C have a value of B *C[y] = l[x-y + N] = r[x + y] =b; $ }Panax Notoginseng};
Leetcode n-queens II Retrospective Search