[Leetcode]51 N-queens

Source: Internet
Author: User

https://oj.leetcode.com/problems/n-queens/

http://blog.csdn.net/linhuanmars/article/details/20667175

public class solution {    public list<string[]>  Solvenqueens (int n)  {                 List<String[]> result = new ArrayList<> ();         int[] qcols = new int[n];         solve (N, 0, qcols, result);         return result;            }         private void solve (Int n, // number of queen                         int row, // current row iteration, [0 ->  n-1]                        int[] qcols, // describing queens positions                        List< String[]> result)     {        if  (row  == n)         {             // all [0, n-1] has fulfilled, a solution  found.            result.add (ToBoard (Qcols)) ;            return;         }                 // we&nbSp;are on row        // try to put q  in each columns, and check        for  ( int i = 0 ; i < n ; i ++)          {            qcols[row] =  i;            boolean valid =  checkboard (Qcols, row);             if   (valid)             {                 // enter next iteration                 solve (n,  Row + 1, qCols, result);            }         }    }        //   This method checks to see if the new Q (ROW) is in line with the existing board     //  because one row only holds the position of a Q, so the line must conform to     //   Check column:qcols[i] != qcols[row]    //  check diagonal  abs (I - row)  != abs (Qcols[i] - qcols[row])     // check new q (on  row)  can fit existing board.    private boolean  Checkboard (Int[] qcols, int row)     {         for  (int i = 0 ; i < row ; i ++)          {             if  (QCOLS[I]&NBSP;==&NBsp;qcols[row])  | |                math.abs (i -  row)  == math.abs (qcols[i] - qcols[row))                 return false;         }        return true;    }     // Build the board.        private  String[] toboard (Int[] qcols)     {         int n = qcols.length;        string[] b  = new String[n];        for  (int i =  0 ; i < n ; i ++)         {             int qcol = qcols[i];             StringBuilder sb = new  StringBuilder ();            for  (int j  = 0 ; j < qcol ; j ++)                  sb.append (".");             sb.append ("Q");             for  (int j = qcol + 1  ; j < n ; j ++)                  sb.append (".");             b[i] = sb.tostring ();         }        return b;     }}


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