- 浏览: 752144 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
u011487470:
感觉就是知识采集一样,博主能不能整理一下
基于Web的IM简介 -
whxtbest:
whxtbest 写道2里面:如果T本身就是重复的话 比如 ...
关于后缀树的一些理解 -
whxtbest:
2里面:如果T本身就是重复的话 比如S是aaab,T是aa ...
关于后缀树的一些理解 -
刘亮love小雪:
谢谢啦
Java 2D高级绘图 -
bluky999:
收集的资料挺多的 哈哈
基于Web的IM简介
回溯法,代码如下:
/**
* 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
/**
* 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
发表评论
-
求交集和并集的线性算法
2011-10-24 23:00 5374对于给定的两个集合,使用哈希表可以在线性时间复杂度内得到他们的 ... -
关于后缀树的一些理解
2008-10-01 21:07 9076要理解suffix tree就首先 ... -
面试题集锦
2008-09-29 17:24 36131. 时针分针重合几次 表面上有60个小格,每小格代表一分钟, ... -
词频统计的C++实现
2008-03-10 16:01 3797#include <map> # ... -
Google面试题(九)
2008-02-20 00:13 279025匹马赛跑,5个跑道,怎么以最少的比赛次数来决出最快的3匹, ... -
数据结构面试大全
2008-02-19 12:50 44971.判断链表是否存在环 ... -
Google面试题(七)
2008-02-14 03:35 3077很多Google考生出来,也 ... -
Google面试题(六)
2008-02-14 03:03 6040题目:对现在的Stack(栈)数据结构进行改进,加一个min( ... -
Google面试题(五)
2008-02-14 02:30 2698几星期前,一个朋友接 ... -
Google面试题(四)
2008-02-13 22:07 229126个英文字母从新排序(未知的顺序alphabet),然后用这 ... -
google面试题(三)
2008-02-13 06:22 2544算法题一:Given 1 GB memory, input a ... -
google面试题(二)
2008-02-11 01:43 3553平面上N个点,求一条直线,穿过的点数最多 思路:2点确定一条 ... -
google面试题(一)
2008-02-11 01:27 2525有一个random number generator,是生成真 ... -
数据结构和算法中容易忽略的要点总结[未完待续]
2008-02-10 23:44 14711。递归需要有个递归出口,否则会是infinite的递归 -
矩阵求逆的快速算法
2008-02-10 20:40 3958算法介绍 矩阵求逆在3D ... -
约瑟夫问题
2008-02-10 09:57 2022有n个人围成一圈,顺序编号。从第一个人开始报数,凡报到m的人退 ... -
hash函数学习总结,以及与hashcode()、hashMap的关系
2008-02-10 06:21 7491以前一直觉得hash函数很 ... -
突然发现没真正领悟哈希表
2008-02-10 05:11 2578在一个字符串中找到第 ... -
[微软面试题]请把一个整形数组中重复的数字去掉
2008-02-10 04:08 4391请把一个整形数组a中重复的数字去掉 方法一: for i ... -
Java版堆排序
2008-02-09 23:41 1204/** * author Akalius Kung 2008 ...
相关推荐
这是一个用Java实现的关于N皇后问题的算法 其中包括回溯和迭代两种算法
本文将深入探讨这一问题,并通过对比Java和C++两种语言的实现,帮助读者更好地理解和应用n皇后算法。 一、n皇后问题的基本概念 1.1 n皇后问题定义 n皇后问题是一个典型的约束满足问题,目标是在n×n的棋盘上摆放n...
**算法用JAVA写的n皇后问题** n皇后问题是一个经典的计算机科学问题,它的目标是在一个n×n的棋盘上放置n个皇后,使得任意两个皇后都不能在同一行、同一列或同一对角线上。这个问题展示了回溯算法的应用,是解决...
n皇后问题的没有同义的解。用java语言实现n-queens算法
《N皇后问题与随机算法解析》 在计算机科学领域,N皇后问题是一个经典的回溯算法应用实例,它源自19世纪的数学家欧拉提出的一个挑战。问题的设定是在一个N×N的棋盘上放置N个皇后,要求任意两个皇后不能处于同一行...
java和c++都有,算法为回溯。n后问题 注:i-j=k-l 或 i+j=k+l 说明2个皇后在对角线上
总结起来,n皇后问题通过回溯算法在Java中的实现,不仅锻炼了我们的逻辑思维能力,也让我们更好地理解了递归和回溯这两种重要的编程技巧。同时,这个问题也为我们提供了一个在有限搜索空间中寻找解的有效方法,这在...
本主题涵盖了几个经典的算法,包括快速排序、广度优先搜索(BFS)、N皇后问题以及最小生成树。下面我们将逐一详细探讨这些算法。 1. **快速排序**:快速排序是一种高效的排序算法,由C.A.R. Hoare在1960年提出。它...
Java编写的N皇后问题是一个经典的计算机编程挑战,它涉及到回溯算法、递归以及问题解决策略。N皇后问题要求在N×N的棋盘上放置N个皇后,使得每个皇后都不能在同一行、同一列或同一斜线上。这个问题的解决方案数量...
总之,N皇后问题的Java实现是一个涉及回溯算法、面向对象编程和图形化界面设计的综合性实例,对于提升编程技能和理解算法有着显著的帮助。通过学习和实践这样的项目,开发者不仅可以掌握基本的编程技巧,还能深入...
在Java中,实现N皇后问题的回溯算法通常会用到二维数组来表示棋盘状态,以及递归函数来遍历所有可能的摆放。每个元素在数组中表示对应位置是否有皇后,递归函数会根据当前行数和已放置的皇后数量来进行判断。 其次...
总的来说,这个项目结合了基本的计算机科学概念,如回溯算法,以及实际的编程技能,如Java GUI编程,提供了一个直观的学习和演示n皇后问题的平台。读者可以通过研究和修改此代码,加深对这两个领域的理解,并锻炼...
遗传算法实现N皇后-java代码
n 皇后问题是一道经典的回溯算法问题,其目标是在一个 � × � n×n 的棋盘上放置 � n 个皇后,使得任意两个皇后都不能在同一行、同一列或同一斜线上。 栈可以用来辅助实现回溯算法,本质上就是手动维护了递归...
通过GUI实现N皇后问题的动态演示,可以使用Java Swing或JavaFX库创建用户界面,展示皇后在棋盘上的位置,并允许用户选择不同数量的皇后进行演示。这样,用户可以直观地看到回溯法如何在棋盘上逐步尝试和撤销皇后的...
与八皇后类似,只是可以输入任意N值。
使用分支限界法解决N皇后问题。因为是广度优先,而且占用比较多的额外空间,所以并不是解N皇后问题的很好的算法,主要是理解分支限界法的使用。
11. **回溯和分支限界**:这类算法用于在大型搜索空间中寻找解决方案,例如八皇后问题、N皇后问题等。 12. **排序算法的时间复杂度分析**:书中会讲解如何分析算法的时间复杂度,帮助读者理解不同算法在不同数据...
对于N皇后问题,Java提供了一种清晰、简洁的语法来实现算法。 第一种解决N皇后问题的算法是回溯法。回溯法是一种试探性的解决问题的方法,当发现当前选择无法达到目标时,会撤销之前的选择,尝试其他路径。在N皇后...