Java版N皇后算法
* author Akalius Kung 2008-2-8
**/
public class Queen {
private int[] grids; // location in each row, index is each row, array value is location of each queen
private int n;
private static int sum;
public Queen() {
init(8);
}
public Queen(int n) {
this.n = n;
grids=new int[n];
for(int i=0;i<n;i++){
grids[i]=0;
}
}
private void init(int n){
grids=new int[n];
for(int i=0;i<n;i++){
grids[i]=0;
}
}
private void layoutQueens(){
this.reBack(0);
}
private void reBack(int t) {
if(t==n){
sum++;
for(int i=0;i<n;i++){
System.out.print("["+i+","+grids[i]+"]"+" ");
if(i==n-1){
System.out.println();
}
}
}
else{
for(int i=0;i<n;i++){
grids[t]=i;
if(isPlace(t)){ // not on the same row or the same col or the diagonal
reBack(t+1); // layout next row
}
}
}
}
private boolean isPlace(int t) { // decide if on the same row or the same col or the diagonal
for(int i=0;i<t;i++){
if((Math.abs(i-t)==Math.abs(grids[i]-grids[t]))||(grids[i]==grids[t])){
return false;
}
}
return true;
}
public static void main(String[] args) {
long start=System.currentTimeMillis();
Queen queen=new Queen(8);
queen.layoutQueens();
long end=System.currentTimeMillis();
System.out.println("time="+(end-start));
System.out.println("sum="+sum);
}
}
输出结果:
[0,0] [1,4] [2,7] [3,5] [4,2] [5,6] [6,1] [7,3]
[0,0] [1,5] [2,7] [3,2] [4,6] [5,3] [6,1] [7,4]
[0,0] [1,6] [2,3] [3,5] [4,7] [5,1] [6,4] [7,2]
[0,0] [1,6] [2,4] [3,7] [4,1] [5,3] [6,5] [7,2]
[0,1] [1,3] [2,5] [3,7] [4,2] [5,0] [6,6] [7,4]
[0,1] [1,4] [2,6] [3,0] [4,2] [5,7] [6,5] [7,3]
[0,1] [1,4] [2,6] [3,3] [4,0] [5,7] [6,5] [7,2]
[0,1] [1,5] [2,0] [3,6] [4,3] [5,7] [6,2] [7,4]
[0,1] [1,5] [2,7] [3,2] [4,0] [5,3] [6,6] [7,4]
[0,1] [1,6] [2,2] [3,5] [4,7] [5,4] [6,0] [7,3]
[0,1] [1,6] [2,4] [3,7] [4,0] [5,3] [6,5] [7,2]
[0,1] [1,7] [2,5] [3,0] [4,2] [5,4] [6,6] [7,3]
[0,2] [1,0] [2,6] [3,4] [4,7] [5,1] [6,3] [7,5]
[0,2] [1,4] [2,1] [3,7] [4,0] [5,6] [6,3] [7,5]
[0,2] [1,4] [2,1] [3,7] [4,5] [5,3] [6,6] [7,0]
[0,2] [1,4] [2,6] [3,0] [4,3] [5,1] [6,7] [7,5]
[0,2] [1,4] [2,7] [3,3] [4,0] [5,6] [6,1] [7,5]
[0,2] [1,5] [2,1] [3,4] [4,7] [5,0] [6,6] [7,3]
[0,2] [1,5] [2,1] [3,6] [4,0] [5,3] [6,7] [7,4]
[0,2] [1,5] [2,1] [3,6] [4,4] [5,0] [6,7] [7,3]
[0,2] [1,5] [2,3] [3,0] [4,7] [5,4] [6,6] [7,1]
[0,2] [1,5] [2,3] [3,1] [4,7] [5,4] [6,6] [7,0]
[0,2] [1,5] [2,7] [3,0] [4,3] [5,6] [6,4] [7,1]
[0,2] [1,5] [2,7] [3,0] [4,4] [5,6] [6,1] [7,3]
[0,2] [1,5] [2,7] [3,1] [4,3] [5,0] [6,6] [7,4]
[0,2] [1,6] [2,1] [3,7] [4,4] [5,0] [6,3] [7,5]
[0,2] [1,6] [2,1] [3,7] [4,5] [5,3] [6,0] [7,4]
[0,2] [1,7] [2,3] [3,6] [4,0] [5,5] [6,1] [7,4]
[0,3] [1,0] [2,4] [3,7] [4,1] [5,6] [6,2] [7,5]
[0,3] [1,0] [2,4] [3,7] [4,5] [5,2] [6,6] [7,1]
[0,3] [1,1] [2,4] [3,7] [4,5] [5,0] [6,2] [7,6]
[0,3] [1,1] [2,6] [3,2] [4,5] [5,7] [6,0] [7,4]
[0,3] [1,1] [2,6] [3,2] [4,5] [5,7] [6,4] [7,0]
[0,3] [1,1] [2,6] [3,4] [4,0] [5,7] [6,5] [7,2]
[0,3] [1,1] [2,7] [3,4] [4,6] [5,0] [6,2] [7,5]
[0,3] [1,1] [2,7] [3,5] [4,0] [5,2] [6,4] [7,6]
[0,3] [1,5] [2,0] [3,4] [4,1] [5,7] [6,2] [7,6]
[0,3] [1,5] [2,7] [3,1] [4,6] [5,0] [6,2] [7,4]
[0,3] [1,5] [2,7] [3,2] [4,0] [5,6] [6,4] [7,1]
[0,3] [1,6] [2,0] [3,7] [4,4] [5,1] [6,5] [7,2]
[0,3] [1,6] [2,2] [3,7] [4,1] [5,4] [6,0] [7,5]
[0,3] [1,6] [2,4] [3,1] [4,5] [5,0] [6,2] [7,7]
[0,3] [1,6] [2,4] [3,2] [4,0] [5,5] [6,7] [7,1]
[0,3] [1,7] [2,0] [3,2] [4,5] [5,1] [6,6] [7,4]
[0,3] [1,7] [2,0] [3,4] [4,6] [5,1] [6,5] [7,2]
[0,3] [1,7] [2,4] [3,2] [4,0] [5,6] [6,1] [7,5]
[0,4] [1,0] [2,3] [3,5] [4,7] [5,1] [6,6] [7,2]
[0,4] [1,0] [2,7] [3,3] [4,1] [5,6] [6,2] [7,5]
[0,4] [1,0] [2,7] [3,5] [4,2] [5,6] [6,1] [7,3]
[0,4] [1,1] [2,3] [3,5] [4,7] [5,2] [6,0] [7,6]
[0,4] [1,1] [2,3] [3,6] [4,2] [5,7] [6,5] [7,0]
[0,4] [1,1] [2,5] [3,0] [4,6] [5,3] [6,7] [7,2]
[0,4] [1,1] [2,7] [3,0] [4,3] [5,6] [6,2] [7,5]
[0,4] [1,2] [2,0] [3,5] [4,7] [5,1] [6,3] [7,6]
[0,4] [1,2] [2,0] [3,6] [4,1] [5,7] [6,5] [7,3]
[0,4] [1,2] [2,7] [3,3] [4,6] [5,0] [6,5] [7,1]
[0,4] [1,6] [2,0] [3,2] [4,7] [5,5] [6,3] [7,1]
[0,4] [1,6] [2,0] [3,3] [4,1] [5,7] [6,5] [7,2]
[0,4] [1,6] [2,1] [3,3] [4,7] [5,0] [6,2] [7,5]
[0,4] [1,6] [2,1] [3,5] [4,2] [5,0] [6,3] [7,7]
[0,4] [1,6] [2,1] [3,5] [4,2] [5,0] [6,7] [7,3]
[0,4] [1,6] [2,3] [3,0] [4,2] [5,7] [6,5] [7,1]
[0,4] [1,7] [2,3] [3,0] [4,2] [5,5] [6,1] [7,6]
[0,4] [1,7] [2,3] [3,0] [4,6] [5,1] [6,5] [7,2]
[0,5] [1,0] [2,4] [3,1] [4,7] [5,2] [6,6] [7,3]
[0,5] [1,1] [2,6] [3,0] [4,2] [5,4] [6,7] [7,3]
[0,5] [1,1] [2,6] [3,0] [4,3] [5,7] [6,4] [7,2]
[0,5] [1,2] [2,0] [3,6] [4,4] [5,7] [6,1] [7,3]
[0,5] [1,2] [2,0] [3,7] [4,3] [5,1] [6,6] [7,4]
[0,5] [1,2] [2,0] [3,7] [4,4] [5,1] [6,3] [7,6]
[0,5] [1,2] [2,4] [3,6] [4,0] [5,3] [6,1] [7,7]
[0,5] [1,2] [2,4] [3,7] [4,0] [5,3] [6,1] [7,6]
[0,5] [1,2] [2,6] [3,1] [4,3] [5,7] [6,0] [7,4]
[0,5] [1,2] [2,6] [3,1] [4,7] [5,4] [6,0] [7,3]
[0,5] [1,2] [2,6] [3,3] [4,0] [5,7] [6,1] [7,4]
[0,5] [1,3] [2,0] [3,4] [4,7] [5,1] [6,6] [7,2]
[0,5] [1,3] [2,1] [3,7] [4,4] [5,6] [6,0] [7,2]
[0,5] [1,3] [2,6] [3,0] [4,2] [5,4] [6,1] [7,7]
[0,5] [1,3] [2,6] [3,0] [4,7] [5,1] [6,4] [7,2]
[0,5] [1,7] [2,1] [3,3] [4,0] [5,6] [6,4] [7,2]
[0,6] [1,0] [2,2] [3,7] [4,5] [5,3] [6,1] [7,4]
[0,6] [1,1] [2,3] [3,0] [4,7] [5,4] [6,2] [7,5]
[0,6] [1,1] [2,5] [3,2] [4,0] [5,3] [6,7] [7,4]
[0,6] [1,2] [2,0] [3,5] [4,7] [5,4] [6,1] [7,3]
[0,6] [1,2] [2,7] [3,1] [4,4] [5,0] [6,5] [7,3]
[0,6] [1,3] [2,1] [3,4] [4,7] [5,0] [6,2] [7,5]
[0,6] [1,3] [2,1] [3,7] [4,5] [5,0] [6,2] [7,4]
[0,6] [1,4] [2,2] [3,0] [4,5] [5,7] [6,1] [7,3]
[0,7] [1,1] [2,3] [3,0] [4,6] [5,4] [6,2] [7,5]
[0,7] [1,1] [2,4] [3,2] [4,0] [5,6] [6,3] [7,5]
[0,7] [1,2] [2,0] [3,5] [4,1] [5,4] [6,6] [7,3]
[0,7] [1,3] [2,0] [3,2] [4,5] [5,1] [6,6] [7,4]
time=270
sum=92