温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

原:八皇后问题的递归和非递归Java实现

发布时间:2020-08-07 16:29:29 来源:ITPUB博客 阅读:200 作者:herosoft 栏目:编程语言

原:八皇后问题的递归和非递归实现

八皇后问题是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名
的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即
任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。

高斯认为有76种方案。1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后
来有人用图论的方法解出92种结果。事实上就是有92种解法。

以下是code:

[@more@]

import java.io.*;
import java.util.*;

class Queens {
final boolean available = true;
final int squares =5, norm = squares - 1;
int[] positionInRow = new int[squares];
boolean[] column = new boolean[squares];
boolean[] leftDiagonal = new boolean[squares*2 - 1];
boolean[] rightDiagonal = new boolean[squares*2 - 1];
int howMany = 0;
List queensList = new ArrayList();

Queens() {
for (int i = 0; i < squares; i++) {
positionInRow[i] = -1;
column[i] = available;
}
for (int i = 0; i < squares*2 - 1; i++)
leftDiagonal[i] = rightDiagonal[i] = available;
}
void printBoard(PrintStream out, int row, int col) {

out.println("row = " +row + ", col = " + col);
}


void putQueen(int row) {
int[] arr = null;
for (int col = 0; col < squares; col++)
if (column[col] == available &&
leftDiagonal [row+col] == available &&
rightDiagonal[row-col+norm] == available) {

positionInRow[row] = col;
column[col] = !available;
leftDiagonal[row+col] = !available;
rightDiagonal[row-col+norm] = !available;
if (row < squares-1)
putQueen(row+1);


else {
for(int kk=0; kk < positionInRow.length; kk++ ) {
System.out.print(positionInRow[kk] +", ");
}
System.out.println();

arr =new int[positionInRow.length];
System.arraycopy(positionInRow, 0, arr, 0, positionInRow.length);
queensList.add(arr);

this.howMany ++;
}

column[col] = available;
leftDiagonal[row+col] = available;
rightDiagonal[row-col+norm] = available;

}
}


void putQueen() {
int times = 1;
boolean flag=false;
int[] st = new int[squares];
int[] st2 = new int[squares];
int top =0;

for(int row=0, col=0; row < squares; ) {
for(; colif (column[col] == available &&
leftDiagonal [row+col] == available &&
rightDiagonal[row-col+norm] == available) {



positionInRow[row] = col;
column[col] = !available;
leftDiagonal[row+col] = !available;
rightDiagonal[row-col+norm] = !available;



st[top]=row;
st2[top]=col;
top++;

col=0;
row++;
flag = true;
break;
}



}

if (row == squares)
for(int k=0; k < positionInRow.length; k++) {
if(positionInRow[k] != -1) {
if(k==positionInRow.length-1) {
for(int kk=0; kk < positionInRow.length; kk++ ) {
System.out.print(positionInRow[kk] +", ");
}
System.out.println();
this.howMany ++;

}
}
}



if(st2[0]==squares-1&&top==0)return;

if(/*col==squares*/ !flag) {
if(/*!st.isEmpty() && !st2.isEmpty()*/top!=0) {
/* row=((Integer)st.pop()).intValue();
col=((Integer) st2.pop()).intValue(); */
top--;row=st[top];col=st2[top];

column[col] = available;
leftDiagonal[row+col] = available;
rightDiagonal[row-col+norm] = available;
col++;
}


}
flag=false;


if(row==squares ) {

row=0;

}





}
}

void getAllSymmetricalQueens() {
int[] q, q2;
for(int i=0; i q = (int[]) queensList.remove(0);

for(int j=0; j q2=(int[]) queensList.get(j);


int k;
for( k=0;kif(q[k]+q2[k]!=squares-1)
break;
}
if(k==squares) {
for(k=0; kSystem.out.print(q[k] + ",");
System.out.print(" and ");
for(k=0; kSystem.out.print(q2[k] + ",");
System.out.print(" are symmetricaln");
}


}
}
}

public static void main(String args[]) {
Queens queens = new Queens();
queens.putQueen(0); System.out.println("----------------");
queens.putQueen();


System.out.println(queens.howMany + " solutions found.");

queens.getAllSymmetricalQueens();
}
}

转载请注明出处:http://herosoft.itpub.net/post/5802/487069

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI