八皇后问题是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。
public class EightQueue {
String[][]rec = new String[8][8];
int[]a = new int[8];
int[]b = new int[15];
int[]c = new int[15];
int sum;
public EightQueue() {
super();
for(int i = 0; i < this.rec.length; i++) {
for(int j = 0; j < this.rec[i].length; j++){
this.rec[i][j]= "○";
}
}
}
public void prt() {
System.out.println("");
for(int i = 0; i < this.rec.length; i++) {
for(int j = 0; j < this.rec[i].length; j++){
System.out.print(this.rec[i][j]+ " ");
}
System.out.println("");
}
System.out.println("");
}
void qu(int i) {
for(int iColumn = 0; iColumn < 8; iColumn++){
if(a[iColumn] == 0 && b[i - iColumn +7] == 0
&&c[i + iColumn] == 0) {
//do not conflict
rec[i][iColumn]= "●";
a[iColumn]= 1;
b[i- iColumn + 7] = 1;
c[i+ iColumn] = 1;
if(i < 7)
qu(i+ 1);
else{
//sysout
prt();
sum++;
}
//whatever how to put the queen, mission is impossible.rollback
rec[i][iColumn]= "○";
a[iColumn]= 0;
b[i- iColumn + 7] = 0;
c[i+ iColumn] = 0;
}
}
}
public static void main(String[] args) {
EightQueue eq = new EightQueue();
eq.qu(0);
System.out.println(eq.sum);
}
}
算法2,也不知道是从哪里剽窃过来的了,该算法没有最终答应出排列组合,仅仅给出有多少种组合,但是算法确实十分奥妙,提供出来大家分享。
package org.luyang.csdn;
public class Queen {
staticint sum = 0, upperlim = 1;
privatestatic void test(int row, int ld, int rd) {
if(row != upperlim) {
intpos = upperlim & ~(row | ld | rd);
while(pos != 0) {
intp = pos & -pos;
pos-= p;
test(row+ p, (ld + p) << 1, (rd + p)>> 1);
}
}else
sum++;
}
publicstatic void main(String[] args) {
intn = 8;
if(args.length == 1)
n= Integer.parseInt(args[0]);
longtm = System.currentTimeMillis();
if((n < 1) || (n > 32)) {
System.out.println("heh..I can't calculate that.");
System.exit(-1);
}
System.out.println(n+ " Queens");
upperlim= (upperlim << n) - 1;
test(0,0, 0);
System.out.println("Numberof solutions is " + sum + ", "
+(System.currentTimeMillis() - tm) + " milliseconds");
}
}