八皇后算法
来源:互联网 发布:证件制作软件手机版 编辑:程序博客网 时间:2024/06/16 14:40
1.下一个棋子后检查算法:
package resultQueue;public class Test { private int[] plate; private int n; private int count; public Gaoziheng(int n) { this.n = n; plate = new int[n]; } public static void main(String args[]) { for (int i = 1; i <= 8; i++) { long start = System.currentTimeMillis(); Gaoziheng gaoziheng = new Gaoziheng(i); gaoziheng.tryPutQueen(); long cost = System.currentTimeMillis() - start; System.out.println(i + "宫格一共耗时" + cost + "ms, 一共" + gaoziheng.count + "种排法"); } } public void tryPutQueen() { tryPutQueen(0); } public void tryPutQueen(int row) { for (int i = 0; i < n; i++) { if (checkPoint(row, i)) { putQueen(row, i); if (row == n - 1) { count++; continue; } tryPutQueen(row + 1);// removeQueen(row); } } } private boolean checkPoint(int row, int col) { for (int i = 0; i < row; i++) { if (plate[i] == col) { return false; } if (Math.abs(plate[i] - col) == Math.abs(row - i)) { return false; } } return true; } public void putQueen(int row, int col) { plate[row] = col; }}
2.通过第一颗和第二课的斜率排除
package resultQueue;public class Test2 { private int[][] plate; private int[] queens; private int n; private int count; public Test2(int n) { this.n = n; plate = new int[n][n]; queens = new int[n]; } public static void main(String args[]) { for (int i = 1; i <= 8; i++) { long start = System.currentTimeMillis(); Liuhanhui liuhanhui = new Liuhanhui(i); liuhanhui.tryPutQueen(); long cost = System.currentTimeMillis() - start; System.out.println(i + "宫格一共耗时" + cost + "ms, 一共" + test2.count + "种排法"); } } public void tryPutQueen() { tryPutQueen(0); } public void tryPutQueen(int row) { for (int i = 0; i < n; i++) { if (plate[row][i] == 0) { putQueen(row, i); if (row == n - 1) { count++; print(); continue; } tryPutQueen(row + 1); removeQueen(row, i); } } } private void print() { StringBuilder sb = new StringBuilder(); for (int i : queens) { sb.append(i).append("\t"); } sb.append("\n"); System.out.println(sb.toString()); } private void removeQueen(int row, int col) { for (int i = row + 1; i < n; i++) { recoverSpace(i, col); recoverSpace(i, col - (i - row)); recoverSpace(i, col + (i - row)); } } public void putQueen(int row, int col) { queens[row] = col; for (int i = row + 1; i < n; i++) { killSpace(i, col); killSpace(i, col - (i - row)); killSpace(i, col + (i - row)); } } public void killSpace(int x, int y) { if (x >= n || y >= n || x < 0 || y < 0) return; plate[x][y] += 1; } public void recoverSpace(int x, int y) { if (x >= n || y >= n || x < 0 || y < 0) return; plate[x][y] -= 1; }}
0 0
- 八皇后算法
- 类似八皇后算法
- 八皇后算法
- 八皇后算法-python
- 八皇后 算法改进
- 趣味算法-八皇后
- 八皇后算法
- 八皇后问题[算法]
- 八皇后 & 迷宫 算法
- 八皇后算法
- 八皇后问题算法
- 算法问题-八皇后
- 算法思考---------八皇后
- 数据结构-八皇后算法
- 分治算法 - 八皇后
- 算法--八皇后问题
- 八皇后算法
- 算法-八皇后问题
- 796A
- 捕鱼来了2017系列游戏
- MyBatis传参数四种方法
- 4.26课堂作业 --转换构造函数
- 【LeetCode】 回溯系列
- 八皇后算法
- Problem F: 分数类的模板数组类
- 线段树
- Windows通过 iis创建FTP服务
- 【ACM】P1000、P10001、P1002、P1003代码演示
- BW 库存管理之processkey
- Comparable和Comparator
- 从此以后,江湖有了它的传说!
- u3d安卓打包