Question: The N-queens puzzle is the problem of placing n queens on a nxn chessboard the that no such two all 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 a empty space respectively.
For example,
There exist two distinct solutions 4-queens to the puzzle:
[
[“. Q.. ",//Solution 1
“... Q ",
"Q..",
“.. Q. "],
[“.. Q. ",//Solution 2
"Q..",
“... Q ",
“. Q.. "]
]
idea:
Use backtracking, that is, from the first line to place the rules, placed in the second row, the third line .... If a row does not have a location that meets the requirements, it returns to the previous line and moves the position backward. So repeatedly, when the last line has a position to be placed, find a solution, the program continues to run, looking for the remaining solution, when the first row of the position is out of range, then there is no more solution.
constraint conditions:
Program End Condition: The first row is positioned more than n
Condition of backtracking: the row data exceeds n
Backtracking needs to be noted: Place the row position at 0
Code Implementation (JAVA):
Import java.util.*; public class Solution {public list<string[]> solvenqueens (int n) {list<string[]> solutionlist = New arraylist<string[]> ()//Hold result if (n==1) {//If only one row, then handle string[] Solution = new Stri
Ng[n];
Solution[0] = "Q";
Solutionlist.add (Solution);
return solutionlist;
} if (n < 4) {//when n equals 2 or 3 o'clock there is no solution return solutionlist; } int[] pos = new Int[n];
Used to hold the position of the Queen for each line int i = 0;
while (pos[0]<n) {///when the position of a row does not exceed the range can continue backtracking, ask for more solutions while (I<n && pos[0]<n) {//Set the location of the Queen for each row while (IsConflict (I,pos) &&pos[i]<n) {//isconflict () to determine whether the position is conflicting pos[i]++;//if the position conflicts, the position moves back
(+ 1)} if (Pos[i]>=n && i>0) {//If a row of data is out of range, return the previous row pos[i]=0;
It is necessary to note that the position of this line is placed 0 i--; pos[i]++;
The position of the previous line after backtracking needs to add 1}else{i++; }} i--; Note the I value is n after the end of the last loop, so you need to subtract 1 if (i = = n-1 && Pos[i] < N) {/////////////////////////
[] Solution = new String[n];
for (int k=0; k<n; k++) {StringBuilder newsolution = new StringBuilder ();
for (int j=0; j< pos[k];j++) {newsolution.append (".");
} newsolution.append ("Q");
for (int j=pos[k]+1;j<n;j++) {newsolution.append (".");
} Solution[k] = Newsolution.tostring ();
} solutionlist.add (Solution);
After finding a solution, you need to continue backtracking and continue looking for more scenarios if (pos[i]<n) {pos[i]++;
}} return solutionlist; } PubLic static Boolean isconflict (int k, int[] pos) {//To determine whether the position meets the requirements for (int i = 0; i<k; i++) {if (pos[k) = = Pos[i] | |
Math.Abs (Pos[k]-pos[i]) = = Math.Abs (k-i)) {return true;
return false; }
}