[Leetcode] N-queens

Source: Internet
Author: User

N-queens

The n-queens Puzzle is the problem of placing N Queens on a nxn chessboard such that No, Queens attack.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens ' placement, where ‘Q‘ and ‘.‘ both Indi Cate a queen and an empty space respectively.

For example,
There exist-distinct solutions to the 4-queens puzzle:

[ [". Q.. ",  //solution 1  " ... Q ",  " Q ... ",".  . Q. "], ["]. Q. ",  //Solution 2  " Q ... ",  " ... Q ",  ". Q.. "]
Analysis:

Save the chessboard as an N-dimensional array a[n], the value of element I in the array represents the empress position of line I, so that the space size of the problem can be compressed dimension O (N), in judging whether conflict is also very simple, first each row only a queen, and in the array occupies only one element position, the row conflict does not exist, Next is the column conflict, judging whether there is a[i] and the current column J to place the Queen is equal. As for the slash conflict, it can be observed that all the queens that clash on the slash have a regular position, i.e. the ranks of each of them are equal in absolute terms, i.e. | Row–i | = | Col–a[i] |.

1 Const intN_queen = +;2 Const intINVALID =- +;3 4 classSolution5 {6 Private:7   BOOLIs_valid (Const intRowConst intColConst intN)8   {9      for(intI=0; i<n; ++i)Ten       if(A[i] = = Col | | abs (I-ROW) = = ABS (a[i]-col)) One         return false; A  -     return true; -   } the  -   voidShow (vector<vector<string> > &ret,intN) -   { -vector<string>temp; +     stringstr =""; -      for(intI=0; i<n; ++i) +     { Astr =""; at        for(intj=0; j<n; ++j) -       { -         if(A[i] = =j) -str + ="Q"; -         Else -str + ="."; in       } -  to temp.push_back (str); +     } -  the Ret.push_back (temp); *   } $ Panax Notoginseng   voidQueen (vector<vector<string> > &ret,intN) -   { the     intI=0, j=0; +  A      while(i<N) the     { +        while(j<N) -       { $         if(Is_valid (i, J, N)) $         { -A[i] =J; -j =0; the            Break; -         }Wuyi         Else theJ + +; -       } Wu  -       if(A[i] = =INVALID) About       { $         if(i = =0) -            Break; -         Else -         { Ai--; +j = A[i] +1; theA[i] =INVALID; -           Continue; $         } the       } the  the       if(i = = n1) the       { - Show (ret, n); inj = A[i] +1; theA[i] =INVALID; the         Continue; About       } thei++; the     } thecout <<"i ="<< I <<"ret.size () ="<< ret.size () <<Endl; +   } -  the  Public:Bayivector<vector<string> > Solvenqueens (intN) the   { the      for(intI=0; i<n; ++i) -A[i] =INVALID; -  thevector<vector<string> >ret; the Queen (ret, n); the  the     returnret; -   } the  the Private: the   intA[n_queen];94};

[Leetcode] 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.