Title:
theN-queens Puzzle is the problem of placingNQueens on anNxNchessboard such that no, 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 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.. "]
Test Instructions:
The question of the eight Queens is to place n pieces on the n x-n chessboard, so that there is no conflict between the pieces and the pieces.
Given an integer n, returns all the different solutions of this eight-queen question.
< Span style= "font-size:12px" > The pieces in each solution are placed in different places, ' Q ' &NBSP;AND&NBSP; ". ' Each represents a pawn and a blank space.
For example
The two different solutions for the 4 Queens question are:
[ [". Q.. ", //solution 1 " ... Q ", " Q ... ",". . Q. "], ["]. Q. ", //Solution 2 " Q ... ", " ... Q ", ". Q.. "]
Algorithm Analysis:8 Queen's question is: so that no queen can directly eat the rest of the queen? To achieve this, neither of the two queens can be in the same row, vertical line, or slash. Reference Blog http://blog.csdn.net/u011095253/article/details/9158473
We divide the problem into several minor problems
1. Traditional DFS recursion
2. Verify that the place where the Queen is placed is legal
3. Output board Results
DFS loops are all possible from the first column to the last column in this line, and if the place is placed through isvalid verification, recursive through Cur+1 to the next line, if not verified, try the next position, if all positions are not valid, jump back to the previous level
The advantage of using int[] is that every time we just change one number, it's like changing the position of the pawn.
The IsValid function, first int[] represents the row, which avoids repeating queen on each line (because you can't put 2 values in an int) simplifies validation and then we just need to verify the columns and diagonals.
When validating a column, verify that the line before the row is duplicated (note that it was before the validation)
Verify diagonal, according to diagonal properties, long = Wide then we are not difficult to write Math.Abs (Loc[i]-loc[cur]) = = (Cur-i)
The last loc[] is the information of the solution (if there is a solution) we convert it to a string, the output board can be
AC Code:
public class Solution {public arraylist<string[]> solvenqueens (int n) {arraylist<string[]> res = New Arraylist<string[]> (); int[] loc = new Int[n]; Record which column The Queen is in, column array dfs (RES,LOC,0,N); return res; } public void Dfs (arraylist<string[]> res, int[] loc, int cur, int n) {if (cur==n) PR Intboard (Res,loc,n); else {for (int i=0;i<n;i++) {Loc[cur] = i; if (IsValid (loc,cur)) DFS (res,loc,cur+1,n); and Queen M+1, if Queen M+1 had finished and returned///two possible: 1: conflict, returned 2. All the queens have been put out and returned safely. Backtrack Queen M to explore new possible or safe locations--->}}} public Boolean isValid ( int[] loc, int cur) {for (int i=0;i<cur;i++)//Just make sure you don't conflict with the queens that are already in place {if (loc[i]==loc[c ur]| | Math.Abs (loc[i]-loc[cur]) = = (cur-i))//Verify Diagonal, according to the diagonal nature, long = wide return false; Then we are not difficult to write Math.Abs (Loc[i]-loc[cur]) = = (cur-i)} return true; } public void Printboard (arraylist<string[]> res, int[] loc, int n) {string[] ans = new string[n]; for (int i=0;i<n;i++) {string row = new String (); for (int j=0;j<n;j++) {if (J==loc[i]) row + = "Q"; else row + = "."; } Ans[i] = row; } res.add (ANS); } }
Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source
[Leetcode] [Java] N-queens