八皇后算法
来源:互联网 发布:速写练线稿软件 编辑:程序博客网 时间:2024/05/21 15:02
我们提供一种遍历的思路,我们可以逐行或者逐列来进行可行摆放方案的遍历,每一行(或列)遍历出一个符合条件的位置,接着就到下一行或列遍历下一个棋子的合适位置,这种遍历思路可以保证我们遍历过程中有一个条件是绝对符合的——就是下一个棋子的摆放位置与前面的棋子不在同一行(或列)。接下来,我们只要判断当前位置是否还符合其他条件,如果符合,就遍历下一行(或列)所有位置,看看是否继续有符合条件的位置,以此类推,如果某一个行(或列)的所有位置都不合适,就返回上一行(或列)继续该行(或列)的其他位置遍历,当我们顺利遍历到最后一行(或列),且有符合条件的位置时,就是一个可行的8皇后摆放方案,累加一次八皇后可行方案的个数,然后继续遍历该行其他位置是否有合适的,如果没有,则返回上一行,遍历该行其他位置,依此下去。这样一个过程下来,我们就可以得出所有符合条件的8皇后摆放方案了。这是一个深度优先遍历的过程,同时也是经典的递归思路。
下面用循环实现
public class EightQueensCirculation { private static final boolean AVAILABLE = true; private int squares = 8, norm = squares - 1; private int positionInRow[] = new int[squares]; private int p = -1; private boolean[] rows = new boolean[squares]; private boolean[] column = new boolean[squares]; private boolean[] leftDiagonal = new boolean[2 * squares - 1]; private boolean[] rightDiagonal = new boolean[2 * squares - 1]; private static int howMany = 0; public EightQueensCirculation() { // To complete the initialization work for the // column,leftDiagonal,rigthDiagonal. for (int i = 0; i < squares; i++) { rows[i] = AVAILABLE; column[i] = AVAILABLE; positionInRow[i] = -1; } for (int i = 0; i < 2 * squares - 1; i++) { leftDiagonal[i] = AVAILABLE; rightDiagonal[i] = AVAILABLE; } } public void printResults(int[] columns) { int row, col; System.out.println("八皇后问题的第 " + howMany + " 种解法"); System.out.print("八皇后问题的结果为:"); for (int e : columns) { System.out.print(e); } System.out.println("\n具体的图示如下图所示:"); for (row = 0; row < squares; row++) { for (col = 0; col < squares; col++) { if (col == positionInRow[row]) { System.out.print("@"); } else { System.out.print("*"); } } System.out.println(); } System.out.println(); } public void putQueen() { int row = 0, col; while (true) { for (col = p + 1; col < squares; col++) { if (rows[row] == AVAILABLE && column[col] == AVAILABLE && leftDiagonal[row + col] == AVAILABLE && rightDiagonal[row - col + norm] == AVAILABLE) { break; } } //在当前的行里面找到了可以放置皇后的位置 if (col < squares) { rows[row] = !AVAILABLE; column[col] = !AVAILABLE; leftDiagonal[row + col] = !AVAILABLE; rightDiagonal[row - col + norm] = !AVAILABLE; positionInRow[row] = col; p = col; } else if (row > 0) {//如果当前行没办反放置皇后了,那么回溯到前一行 row--; p = positionInRow[row]; rows[row] = AVAILABLE; column[p] = AVAILABLE; leftDiagonal[row + p] = AVAILABLE; rightDiagonal[row - p + norm] = AVAILABLE; positionInRow[row] = -1; continue; } else { break; } if (row == squares - 1) { howMany += 1; printResults(positionInRow); p = positionInRow[row]; rows[row] = AVAILABLE; column[p] = AVAILABLE; leftDiagonal[row + p] = AVAILABLE; rightDiagonal[row - p + norm] = AVAILABLE; positionInRow[row] = -1; continue; } else { row++; p = -1; continue; } } } public static void main(String args[]) { EightQueensCirculation eightQueens = new EightQueensCirculation(); eightQueens.putQueen(); System.out.println("皇后问题一共有" + howMany + "种解法"); }}
下面用递归实现
public class EightQueensRecursive { private static final boolean AVAILABLE = true; private int squares = 8, norm = squares-1; private int positionInRow[] = new int[squares]; private boolean[] column = new boolean[squares]; private boolean[] leftDiagonal = new boolean[2*squares-1]; private boolean[] rightDiagonal = new boolean[2*squares-1]; private static int howMany = 0; public EightQueensRecursive() { //To complete the initialization work for the column,leftDiagonal,rigthDiagonal. for(int i=0; i<squares; i++){ column[i] = AVAILABLE; positionInRow[i] = -1; } for(int i=0; i<2*squares-1; i++){ leftDiagonal[i] = AVAILABLE; rightDiagonal[i] = AVAILABLE; } } public void printResults(int[] columns) { int row,col; System.out.println("八皇后问题的第 "+howMany+" 种解法"); System.out.print("八皇后问题的结果为:"); for(int e:columns) { System.out.print(e); } System.out.println("\n具体的图示如下图所示:"); for(row=0; row<squares; row++) { for(col=0; col<squares; col++) { if(col == positionInRow[row]) { System.out.print("@"); } else { System.out.print("*"); } } System.out.println(); } System.out.println(); } public void putQueen(int row) { //如果前面已经得到了一个可行解 for(int i=0; i<squares; i++) { if(row>squares-1) break; if(column[i] == AVAILABLE && leftDiagonal[row+i] == AVAILABLE && rightDiagonal[row-i+norm] == AVAILABLE) { positionInRow[row] = i; column[i] = !AVAILABLE; leftDiagonal[row+i] = !AVAILABLE; rightDiagonal[row-i+norm] = !AVAILABLE; if(row < squares-1) { putQueen(row+1); } else { howMany += 1; printResults(positionInRow); } column[i] = AVAILABLE; leftDiagonal[row+i] = AVAILABLE; rightDiagonal[row-i+norm] = AVAILABLE; } } } public static void main(String args[]) { EightQueensRecursive eightQueens = new EightQueensRecursive(); eightQueens.putQueen(0); System.out.println("皇后问题一共找到了 "+howMany+"组解。"); }}
0 0
- 八皇后算法
- 类似八皇后算法
- 八皇后算法
- 八皇后算法-python
- 八皇后 算法改进
- 趣味算法-八皇后
- 八皇后算法
- 八皇后问题[算法]
- 八皇后 & 迷宫 算法
- 八皇后算法
- 八皇后问题算法
- 算法问题-八皇后
- 算法思考---------八皇后
- 数据结构-八皇后算法
- 分治算法 - 八皇后
- 算法--八皇后问题
- 八皇后算法
- 算法-八皇后问题
- select下拉框之获取显示值问题
- UI设计常用的图像格式及特点太原UI设计师郭文军分享给你
- 不同宽度显示器下网页宽度的自适应
- 工厂模式的block回调(使用简单方便)
- 自定义开关Button
- 八皇后算法
- super_vars.php ref_var.php
- Android Studio使用相关博客汇总
- HDU3555 Bomb (数位dp)
- 怎样能在大小不同的屏幕上显示同样的网页(自适应网页设计)?
- Android系统下的动态Dex加载
- PAT (Advanced Level) Practise 1057 Stack (30)
- Javabean
- 了解:XSS跨站脚本攻击原理及常用的防护方法(转载)