Java N-Queen implementation problem parsing _java

Source: Internet
Author: User
Tags int size
The N-Queens problem is a typical constraint solving problem, which can be quickly obtained by using the recursive mechanism.
n the description of the Queen question:
On a n*n chessboard, placing n queens, requiring each queen to be in rows, columns, and two diagonal lines without any other queens, otherwise the Queens will attack each other. As shown in the following figure.

Using recursive mechanism, it is easy to solve N-Queens problem. For the eight queens, there are altogether 92 kinds of solutions. The general solution code for the N-Queens problem is given below, where the code is coded using Java.
A total of three classes have been designed, one is the Queen Class (Queen), one Board class (Board), and one is the Solver main program Class (Nqueens). The specific code is as follows:
Copy Code code as follows:

Import java.util.ArrayList;
Import java.util.List;
public class Nqueens {
private int numsolutions;//Number of solution results
private int queensize;//Queen's number
Private Board board;//Chessboard
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) {
Place the Queen's position in this column
Queens.get (Row). SetPosition (COL);
Then add the appropriate position (just below the column and two diagonal lines) to 1 (even if it is not available)
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);
}
Backtrack, minus 1 for the corresponding position (just below the column and 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 solution Results
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;
The position of the Queen
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 class
public class Board implements Serializable,cloneable {
/**
*
*/
Private static final long serialversionuid = -2530321259544461828l;
The size of the chessboard
private int queensize;
The layout of the chessboard
Private int[][] layout;
public Board (int size) {
this.queensize = size;
This.layout = new Int[queensize][queensize];
Initialize so that all the board positions are available, that is, all 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.