[Leetcode]51. N-QUEENSN Queen

Source: Internet
Author: User

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.

Example:

Input:4output: [[". Q.. ",  //solution 1  " ... Q ",  " Q ... ",".  . Q. "], ["]. Q. ",  //Solution 2  " Q ... ",  " ... Q ",  ". Q.. "] Explanation:there exist, distinct solutions to the 4-queens puzzle as shown above.

Topic

NxN chessboard pendulum n pieces, require not to peer, the same column, the same diagonal, the same counter-diagonal, return all pendulum method.

Ideas

Dfs:c[i] denotes the column number where the queen of the line I is located, i.e. a queen is placed on the position (I, c[i]) so that a one-dimensional array can be used to record the entire board.

Code

1 /* 2 Time:o (n!*n) n rows * per line from N to N-1 to n-2 ... 1 i.e. n!3 space:o (n)4 */5 6 classSolution {7        PublicList<list<string>> Solvenqueens (intN) {8list<list<string>> result =NewArraylist<>();9         int[] C =New int[n];//C[i] Represents the column number of the Queen I row, from two to one-dimensionalTenDFS (C, 0, result); One         returnresult; A     } -     Private Static voidDfsint[] C,intRow, list<list<string>>result) { -         intN =c.length; the         if(row = = N) {//The termination condition, also the convergence condition, means that a feasible solution has been found -List<string> solution =NewArraylist<>(); -             //line I -              for(inti = 0; i < N; ++i) { +                 Char[] Chararray =New Char[N]; -Arrays.fill (Chararray, '. ')); +                 //Column J A                  for(intj = 0; J < N; ++j) { at                     if(j = = C[i]) chararray[j] = ' Q '; -                 } -Solution.add (NewString (Chararray)); -             } - Result.add (solution); -             return; in         } -  to          for(intj = 0; J < N; ++J) {//extended status, one column of the test +             BooleanOK =isValid (C, Row, j); -             if(!ok)Continue;//pruning, if illegal, continue to try the next column the             //Perform extended actions *C[row] =J; $DFS (C, row + 1, result);Panax Notoginseng             //Undo Action -             //C[row] =-1; the         } +     } A  the     /** + * Can you put a queen in the (row, col) position? -      * $      * @paramC Chess $      * @paramRow is currently being processed, and the front row has been put to the Queen. -      * @paramCol Current Column -      * @returncan you put a queen ? the      */ -     Private Static BooleanIsValid (int[] C,intRowintCol) {Wuyi          for(inti = 0; i < row; ++i) { the             //in the same column -             if(C[i] = = col)return false; Wu             //on the same diagonal -             if(Math.Abs (i-row) = = Math.Abs (c[i]-col))return false; About         } $         return true; -     } -}

[Leetcode]51. N-QUEENSN Queen

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.