标签:
public class NQueens {
public static int num = 0; // 累计方案总数
public static final int MAXQUEEN = 5;// 皇后个数,同时也是棋盘行列总数
public static int[] cols = new int[MAXQUEEN]; // 定义cols数组,表示n列棋子摆放情况
public NQueens() {
// 核心函数,从第0列开始
getArrangement(0);
System.out.println(MAXQUEEN + "皇后问题有" + num + "种摆放方法。");
}
public void getArrangement(int n) {
// 遍历该列所有不合法的行,并用rows数组记录,不合法即rows[i]=true
boolean[] rows = new boolean[MAXQUEEN];
for (int i = 0; i < n; i++) {
rows[cols[i]] = true;
int d = n - i;
if (cols[i] - d >= 0)
rows[cols[i] - d] = true;
if (cols[i] + d <= MAXQUEEN - 1)
rows[cols[i] + d] = true;
}
for (int k = 0; k < MAXQUEEN; k++) {
// 判断该行是否合法
if (rows[k])
continue;
// 设置当前列合法棋子所在行数
cols[n] = k;
// 当前列不为最后一列时
if (n < MAXQUEEN - 1) {
getArrangement(n + 1);
} else {
// 累计方案个数
num++;
// 打印棋盘信息
printChessBoard();
}
}
}
public void printChessBoard() {
System.out.println("第" + num + "种走法");
for (int i = 0; i < MAXQUEEN; i++) {
for (int j = 0; j < MAXQUEEN; j++) {
if (i == cols[j]) {
System.out.print("0 ");
} else
System.out.print("+ ");
}
System.out.println();
}
}
public static void main(String args[]) {
NQueens queen = new NQueens();
}
}
参考文献
http://blog.csdn.net/zhong317/article/details/4586131
http://zh.wikipedia.org/wiki/%E5%85%AB%E7%9A%87%E5%90%8E%E9%97%AE%E9%A2%98
标签:
原文地址:http://blog.csdn.net/hellozpc/article/details/45506825