八皇后问题
http://www.diybl.com/ 2008-10-8 网络 点击:
[ 评论 ]
文章搜索:
【点击打包该文章】
package org.luyang.csdn;
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
八皇后问题是一个古老而著名的问题,是回溯算法的典型例题。该问题是19世纪著名的数学家高斯1850年提出:在8×8格的国际象棋盘上摆放8个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。[英国某著名计算机图形图像公司面试题]
算法1:典型的回朔算法。打印出8皇后的最终排列。
解析:递归实现n皇后问题。
算法分析:
数组a、b、c分别用来标记冲突,a数组代表列冲突,从a[0]~a[7]代表第0列到第7列。如果某列上已经有皇后,则为1,否则为0。
数组b代表主对角线冲突,为b[i-j+7],即从b[0]~b[14]。如果某条主对角线上已经有皇后,则为1,否则为0。
数组c代表从对角线冲突,为c[i+j],即从c[0]~c[14]。如果某条从对角线上已经有皇后,则为1,否则为0。
package org.luyang.csdn;
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
如果图片或页面不能正常显示请点击这里 站内搜索:
推荐文章 |
