Java N queen implements problem resolution
- 2020-04-01 01:27:11
- OfStack
The N queen problem is a typical constraint problem, which can be solved quickly by using the recursive mechanism.
Description of queen N problem:
On an n by n chessboard, n queens are placed. No other queens are allowed in the rows, columns, and diagonals of each queen, or they will attack each other. As shown in the following figure.
The n queen problem can be solved easily by using the recursive mechanism. For the eight queens, there are 92 solutions. The following is the general solution code for the n-queen problem, where the code is coded in Java.
A total of three classes are designed, one is the Queen class, one is the Board class, one is the main program class (NQueens). The specific code is as follows:
Description of queen N problem:
On an n by n chessboard, n queens are placed. No other queens are allowed in the rows, columns, and diagonals of each queen, or they will attack each other. As shown in the following figure.
The n queen problem can be solved easily by using the recursive mechanism. For the eight queens, there are 92 solutions. The following is the general solution code for the n-queen problem, where the code is coded in Java.
A total of three classes are designed, one is the Queen class, one is the Board class, one is the main program class (NQueens). The specific code is as follows:
import java.util.ArrayList;
import java.util.List;
public class NQueens {
private int numSolutions;//Number of results
private int queenSize;//How many queens
private Board board;//The board
private List<Queen> queens = new ArrayList<Queen>();//The 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 position of the queen in this column position
queens.get(row).setPosition(col);
//Then add 1 to the corresponding position (right below the column and two diagonals) (even if it's 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);
}
//Go back and subtract 1 from the corresponding position (right below the column and two diagonals)
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
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();
}
public static void main(String[] args) {
//You can change the parameters
NQueens nQueens = new NQueens(8);
nQueens.solve();
}
}
import java.io.Serializable;
//The queen's class
public class Queen implements Serializable, Cloneable {
private static final long serialVersionUID = 7354459222300557644L;
//Queen's position
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;
//A board
public class Board implements Serializable,Cloneable {
private static final long serialVersionUID = -2530321259544461828L;
//Size of board
private int queenSize;
//Checkerboard layout
private int[][] layout;
public Board(int size){
this.queenSize = size;
this.layout = new int[queenSize][queenSize];
//Initialize so that all positions on the board are 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();
}
}