In the morning to think of an algorithm problem, think of the eight queen recently saw the problem, on the Internet search information, also according to the algorithm written by the previous one side, I think the structure is very clear, recursive backtracking is very convenient to solve the problem. Now put the code and comments on your own to review and summarize.
A. Description of the problem:
Place the N-Queens that are not attacked by each other on the board of the n*n grid. According to the rules of chess, the Queen can attack pieces that are on the same line or in the same column or on the same slash. n the problem is equivalent to placing n queens on the re-NXN board, any 2 queens may be on the same row or column or on the same slash.
Input:
The size of the given chessboard N
Output:
All Placement Methods
two. Solving problems Ideas:
To solve the problem of the N queen is to solve how to place the N queen position, that is, each queen cannot be in the same row, the same column or the same slash with all previous queens. n rows to put n queens, not on the same line, obviously only one queen per line, then we can use this as a known condition, only consider how each column should be put, obviously need and only need to meet two conditions:
1. Cannot be in the same column as all previous Queens
2. Cannot be in the same slash as all previous queens.
data structure can be used with a rraylist<integer> storage a placement position that satisfies the condition , that is, only need to record the number of columns per row, the number of rows is ARRAYLIST<INTEGER> . Size () . All conditions of placement can also be used with a arraylist<string>, which means that each The ToString () result of the position that satisfies the condition. For a detailed algorithm, see Code:
< Span style= "FONT-FAMILY:HELVETICA,TAHOMA,ARIAL,SANS-SERIF; font-size:14px; line-height:25.2000007629395px ">
Package Code;import java.util.arraylist;import java.util.iterator;/** * Solve n Queen problem with recursive backtracking algorithm * @author Chenhaiyue * November 26, 2014 10:50:47 */public class Nqueen {public static void main (string[] args) {arraylist<integer> array = new Ar Raylist<integer> (); arraylist<string> Arrayresult = new arraylist<string> (); int n = 11;returnnqueen (array, Arrayresult, n); SYSTEM.OUT.PRINTLN ("Total number of solutions:" + arrayresult.size ());iterator<string> ITR = Arrayresult.iterator (); Itr.hasnext ();) {System.out.println (Itr.next ());}} /** * Calculates all the solutions of the N Queen problem * @param array current placement of all queens * @param arrayresult of all possible solutions * @param n */private static void Returnnqueen ( arraylist<integer> array,arraylist<string> Arrayresult, int n) {//If you get a solution, put it into the set of all Solutions if (array.size () = = N ) {Arrayresult.add (array.tostring ());} for (int i = 0; i < n; i++) {if (Checknqueen (array, i)) {array.add (i); Returnnqueen (array, Arrayresult, n); Array.remove ( Array.size ()-1);}}} /** * Check if the position of Queen K is feasible, neither of them is feasible. 1: In the same column already exists Queen 2: in theThe corner line already exists Queen * @param array current position of all queens * @param k k line * @return */private static Boolean Checknqueen (Arraylist<integer> ; Array, int k) {int n = array.size (); for (int i = 0; i < n; i++) {//First case if (k = = Array.get (i)) return false;//second case if (n -I = = Math.Abs (K-array.get (i))) return false;} return true;}}
N Queen question Java implementation