Analysis of java N queen's implementation problems

Source: Internet
Author: User

The N queen problem is a typical constraint-solving problem. The recursive mechanism can be used to quickly obtain results.
N queen's question description:
There are n queens on a n * n board, and each queen must be in the row, column, and two diagonal lines, otherwise, these queens will attack each other. As shown in.

The recursive mechanism can be used to easily solve the n queen problem. There are 92 solutions for the eight queens. The following is a general solution code for the N-queen problem. The code here uses java encoding.
Three classes are designed: Queen, Board, and NQueens ). The Code is as follows: Copy codeThe Code is as follows: import java. util. ArrayList;
Import java. util. List;
Public class NQueens {
Private int numSolutions; // Number of solution results
Private int queenSize; // The number of queens
Private Board; // board
Private List <Queen> queens = new ArrayList <Queen> (); // Queen
// Private List <Queen> nQueens = new ArrayList <Queen> ();
Public NQueens (){
}
Public NQueens (int size ){
NumSolutions = 0;
QueenSize = size;
Board = new Board (queenSize );
For (int I = 0; I <queenSize; I ++ ){
Queen queen = new Queen ();
Queens. add (queen );
}
}
Public void solve (){
System. out. println ("Start solve ....");
PutQueen (0 );
}
Private void putQueen (int index ){
Int row = index;
// If this column is available
For (int col = 0; col <board. getQueenSize (); col ++ ){
If (board. getLayout () [row] [col] = 0 ){
// Set the Queen's position to the column's position
Queens. get (row). setPosition (col );
// Then add the corresponding position (bottom of the column and two diagonal lines) to 1 (even if it is unavailable)
For (int I = row + 1; I <board. getQueenSize (); I ++ ){
Board. getLayout () [I] [col] ++;
If (row + col-I> = 0 ){
Board. getLayout () [I] [row + col-I] ++;
}
If (I + col-row <board. getQueenSize ()){
Board. getLayout () [I] [I + col-row] ++;
}
}
If (row = board. getQueenSize ()-1 ){
NumSolutions ++;
PrintSolution (numSolutions );
} Else {
PutQueen (row + 1 );
}
// Trace back and subtract 1 from the corresponding position (the bottom of the column and the two diagonal lines)
For (int I = row + 1; I <board. getQueenSize (); I ++ ){
Board. getLayout () [I] [col] --;
If (row + col-I> = 0 ){
Board. getLayout () [I] [row + col-I] --;
}
If (I + col-row <board. getQueenSize ()){
Board. getLayout () [I] [I + col-row] --;
}
}
}
}
}
// Print the solution result
Private void printSolution (int I ){
System. out. println ("The" + I + "solution is :");
For (int j = 0; j <board. getQueenSize (); j ++ ){
For (int k = 0; k <board. getQueenSize (); k ++ ){
System. out. print (queens. get (j). getPosition () = k? "*":"-");
}
System. out. println ();
}
System. out. println ();
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// You can change the parameters.
NQueens nQueens = new NQueens (8 );
NQueens. solve ();
}
}
Import java. io. Serializable;
// Queen class
Public class Queen implements Serializable, Cloneable {
/**
*
*/
Private static final long serialVersionUID = 7354459222300557644L;
// Queen's location
Private int position;
Public Queen (){
}
Public int getPosition (){
Return position;
}
Public void setPosition (int position ){
This. position = position;
}
Public Object clone () throws CloneNotSupportedException {
Return super. clone ();
}
}
Import java. io. Serializable;
// Board
Public class Board implements Serializable, Cloneable {
/**
*
*/
Private static final long serialVersionUID =-2530321259544461828L;
// Size of the Board
Private int queenSize;
// Board Layout
Private int [] [] layout;
Public Board (int size ){
This. queenSize = size;
This. layout = new int [queenSize] [queenSize];
// Initialize to make all positions on the board available, that is, set all to 0
For (int I = 0; I <queenSize; I ++ ){
For (int j = 0; j <queenSize; j ++ ){
Layout [I] [j] = 0;
}
}
}
Public int getQueenSize (){
Return queenSize;
}
Public void setQueenSize (int queenSize ){
This. queenSize = queenSize;
}
Public int [] [] getLayout (){
Return layout;
}
Public void setLayout (int [] [] layout ){
This. layout = layout;
}
Public Object clone () throws CloneNotSupportedException {
Return super. clone ();
}
}

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.