八皇后算法

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;
    }

}

你可能感兴趣的:(八皇后,算法,javaSe)