`

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
分享到:
评论

相关推荐

    Java实现N皇后问题的算法

    这是一个用Java实现的关于N皇后问题的算法 其中包括回溯和迭代两种算法

    n皇后算法解读Java版和C++版对比-动力节点共16页

    本文将深入探讨这一问题,并通过对比Java和C++两种语言的实现,帮助读者更好地理解和应用n皇后算法。 一、n皇后问题的基本概念 1.1 n皇后问题定义 n皇后问题是一个典型的约束满足问题,目标是在n×n的棋盘上摆放n...

    算法用JAVA写的n皇后问题

    **算法用JAVA写的n皇后问题** n皇后问题是一个经典的计算机科学问题,它的目标是在一个n×n的棋盘上放置n个皇后,使得任意两个皇后都不能在同一行、同一列或同一对角线上。这个问题展示了回溯算法的应用,是解决...

    n皇后问题java解

    n皇后问题的没有同义的解。用java语言实现n-queens算法

    N皇后随机算法

    《N皇后问题与随机算法解析》 在计算机科学领域,N皇后问题是一个经典的回溯算法应用实例,它源自19世纪的数学家欧拉提出的一个挑战。问题的设定是在一个N×N的棋盘上放置N个皇后,要求任意两个皇后不能处于同一行...

    n_queen.rar_N皇后_N皇后 java_n个皇后算法_n后回溯_queen

    java和c++都有,算法为回溯。n后问题 注:i-j=k-l 或 i+j=k+l 说明2个皇后在对角线上

    n后问题回溯算法 java

    总结起来,n皇后问题通过回溯算法在Java中的实现,不仅锻炼了我们的逻辑思维能力,也让我们更好地理解了递归和回溯这两种重要的编程技巧。同时,这个问题也为我们提供了一个在有限搜索空间中寻找解的有效方法,这在...

    java实现一些基本的算法,如快速排序,BFS,N皇后,最小生成树等

    本主题涵盖了几个经典的算法,包括快速排序、广度优先搜索(BFS)、N皇后问题以及最小生成树。下面我们将逐一详细探讨这些算法。 1. **快速排序**:快速排序是一种高效的排序算法,由C.A.R. Hoare在1960年提出。它...

    Java编写的N皇后问题

    Java编写的N皇后问题是一个经典的计算机编程挑战,它涉及到回溯算法、递归以及问题解决策略。N皇后问题要求在N×N的棋盘上放置N个皇后,使得每个皇后都不能在同一行、同一列或同一斜线上。这个问题的解决方案数量...

    NQueen N皇后问题java实现

    总之,N皇后问题的Java实现是一个涉及回溯算法、面向对象编程和图形化界面设计的综合性实例,对于提升编程技能和理解算法有着显著的帮助。通过学习和实践这样的项目,开发者不仅可以掌握基本的编程技巧,还能深入...

    N皇后问题,带界面的 java版

    在Java中,实现N皇后问题的回溯算法通常会用到二维数组来表示棋盘状态,以及递归函数来遍历所有可能的摆放。每个元素在数组中表示对应位置是否有皇后,递归函数会根据当前行数和已放置的皇后数量来进行判断。 其次...

    n皇后问题(java含界面)

    总的来说,这个项目结合了基本的计算机科学概念,如回溯算法,以及实际的编程技能,如Java GUI编程,提供了一个直观的学习和演示n皇后问题的平台。读者可以通过研究和修改此代码,加深对这两个领域的理解,并锻炼...

    八皇后问题遗传算法java实现

    遗传算法实现N皇后-java代码

    用栈求解n皇后问题 ,经典的回溯算法问题

    n 皇后问题是一道经典的回溯算法问题,其目标是在一个 � × � n×n 的棋盘上放置 � n 个皇后,使得任意两个皇后都不能在同一行、同一列或同一斜线上。 栈可以用来辅助实现回溯算法,本质上就是手动维护了递归...

    回溯法解决N皇后问题 Java代码实现

    通过GUI实现N皇后问题的动态演示,可以使用Java Swing或JavaFX库创建用户界面,展示皇后在棋盘上的位置,并允许用户选择不同数量的皇后进行演示。这样,用户可以直观地看到回溯法如何在棋盘上逐步尝试和撤销皇后的...

    N皇后问题

    与八皇后类似,只是可以输入任意N值。

    分支限界法解决N皇后问题

    使用分支限界法解决N皇后问题。因为是广度优先,而且占用比较多的额外空间,所以并不是解N皇后问题的很好的算法,主要是理解分支限界法的使用。

    Java数据结构和算法中文第二版_Java数据结构_

    11. **回溯和分支限界**:这类算法用于在大型搜索空间中寻找解决方案,例如八皇后问题、N皇后问题等。 12. **排序算法的时间复杂度分析**:书中会讲解如何分析算法的时间复杂度,帮助读者理解不同算法在不同数据...

    解决N皇后问题

    对于N皇后问题,Java提供了一种清晰、简洁的语法来实现算法。 第一种解决N皇后问题的算法是回溯法。回溯法是一种试探性的解决问题的方法,当发现当前选择无法达到目标时,会撤销之前的选择,尝试其他路径。在N皇后...

Global site tag (gtag.js) - Google Analytics