八皇后算法

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值