N-queens

The N-queens puzzle is the problem of placing n queens in an NXN chessboard such, that no, and Queens attack each other.

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 indicate 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.. "]

]

Idea: The big way I think of two ideas, the first one is to transform the N queen problem into a full permutation problem. Set X[n] As a set of solutions, the Queen is placed on behalf of row I, section x[i] column.

So find out the whole arrangement and then judge whether it is legal.

The code is as follows (without AC, which should be AC after further optimization):

public class Solution {public list<list<string>> solvenqueens (int n) {LIST<LIST<STRING>&G T returnlist = new arraylist<list<string>> (); /** * X[i] for the location of column I * converted to 0-(n-1) of the full arrangement, and then determine whether the full alignment of the position is valid */int[] num = new Int[n]; int i = 0; while (I < n) {num[i] = i;//padding array is 0-(n-1) i++; } list<list<integer>> List = Permuteunique (num); for (list<integer> al:list) {//per group of data processing for (i = 0; i < al.size (); i++) {if (!check (Al, I, Al.get (i)) break;//not valid out} if (i = = N) {///to last, stating all legal list<string> ls = new Arraylist<string> ;(); for (i = 0; i < al.size (); i++) {string s = "";//String for per group (int j = 0; J < n;j++) {if (j = = Al.get (i)) {s + = "Q";//Place Queen's position} else{s + = "."; /Place}} ls.add (s);//Add to LS} returnlist.add (LS);//Add a set of solutions }} return returnlist; }/** * Determine if it is legal * @param al a set of solutions * @param i current row * @param x Current column * @return Legal */boolean check (list<integer> al,int i,int x) {for (int k = 0; k < i; k++) {//only compared with above, otherwise if (Al.get (k) = = X | | x-i = = al.get (k)-K | | x + i = = Al.get (k) + K) return false; }return true; }//Perfection Arrange public static list<list<integer>> permuteunique (int[] num) {list<list<integer>> returnlist = new arraylist<list<integer>> (); Returnlist.add (new arraylist<integer> ()); for (int i = 0; i < num.length; i++) {set<list<integer>> currentset = new Hashset<list<integer>> ;(); for (list<integer> l:returnlist) {for (int j = 0; J < l.size () + 1; j + +) {L.add (J, Num[i]); list<integer> T = new arraylist<integer> (l); L.remove (j); Currentset.add (t);}} returnlist = new arraylist<list<integer>> (currentset);} return returnlist;}}

Another way of thinking is backtracking, in line with the requirements of the go down, do not meet the requirements of the backward.

Specific code:

public class Solution {/** * The whole idea of this question is to build an array of x[n], meaning the first place in the X[i] column to place the Queen's legal judgment rule is the column is not equal, the slash is not equal * i + x[i] = = j + X[J] denotes a forward slash consistent * X[i]-i = x[j]-J means that a negative slash is not consistent on the legal */list<list<string>> list;//save result public list<list <String>> solvenqueens (int n) {list = new arraylist<list<string>> (); int[] x = new int[n];//save result Quee NS (x, n, 0); return list;} void Queens (int[] x,int n,int row) {for (int i = 0; i < n; i++) {if (check (x,n,row,i)) {//Judge legal x[row] = i;//Place queen on row row, column I if ( row = = n-1) {///If it is the last row, the output addlist (x,n); X[row] = 0;//backtracking, looking for the next result return;} Queens (x, N, row+1);//Look for the next line x[row] = 0;//back}}}/** * Add the results of each group to the list * @param x Array Solution * @param n checkerboard width */private void addlist (int [] x,int N) {//Add result string[][] SArr = new String[n][n]; List<string> al = new arraylist<string> (); for (int i = 0; i < n; i++) {Arrays.fill (Sarr[i], "."); /fill in. sarr[i][x[i]] = "Q";//position qstring s = ""; for (String Str:sarr[i]) {s + = str;//add together}al.add (s);//Add one row}list.add (AL) ;//Add a set of solutions}/** * @param x Array Solution * @param n Checkerboard Width * @param index currently placed row * @param i currently place column * @return */boolean check (int[] x,int n,int row, int Col) {for (int i = 0; i < row; i++) {//Because the rows are not equal, determine if the columns are equal, if the slashes are equal if (x[i] = Col | | x[i] + i = col + row | | x[i]-i = = C Ol-row) return false;} return true;}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 51.n-queens (N queen problem) thought and method of solving problems