Leetcode n-queens II Retrospective Search

Source: Internet
Author: User

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

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.