java N皇后实现问题解析
N皇后问题是一个典型的约束求解问题,利用递归机制,可以很快的得到结果。
N皇后问题的描述:
在一个n*n的棋盘上,摆放n个皇后,要求每个皇后所在行、列、以及两个对角线上不能出现其他的皇后,否则这些皇后之间将会相互攻击。如下图所示。
利用递归机制,可以很容易的求解n皇后问题。针对八皇后,总共有92种解。下面将给出N-皇后问题的一般求解代码,在这里代码是使用java编码的。
总共设计了三个类,一个是皇后类(Queen),一个棋盘类(Board),一个是求解主程序类(NQueens)。具体代码如下:
代码如下:
import java.util.ArrayList;
import java.util.List;
public class NQueens {
private int numSolutions;//求解结果数量
private int queenSize;//皇后的多少
private Board board;//棋盘
private List<Queen> queens = new ArrayList<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;
//如果此列可用
for (int col = 0; col < board.getQueenSize(); col++) {
if (board.getLayout()[row][col] == 0) {
//将皇后的位置置为此列位置
queens.get(row).setPosition(col);
//然后将相应的位置(此列的正下方以及两个对角线)加1(即使其不可用)
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);
}
//回溯,将相应的位置(此列的正下方以及两个对角线)减1
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] --;
}
}
}
}
}
//打印求解结果
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) {
//可以改变参数
NQueens nQueens = new NQueens(8);
nQueens.solve();
}
}
import java.io.Serializable;
//皇后类
public class Queen implements Serializable, Cloneable {
/**
*
*/
private static final long serialVersionUID = 7354459222300557644L;
//皇后的位置
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;
//棋盘类
public class Board implements Serializable,Cloneable {
/**
*
*/
private static final long serialVersionUID = -2530321259544461828L;
//棋盘的大小
private int queenSize;
//棋盘的布局
private int[][] layout;
public Board(int size){
this.queenSize = size;
this.layout = new int[queenSize][queenSize];
//初始化,使棋盘所有位置都可用,即全部置为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();
}
}