尚硅谷Java数据结构与算法——047 八皇后问题

代码实现

package com.atguigu.recursion;

/**
 * @author WHT
 * @create 2021-06-14 9:00
 */
public class Queue8 {

    // 定义一个max表示共有多少个皇后。
    int max = 8;
    // 定义数组array, 保存皇后放置位置的结果,比如 arr = {0, 4, 7, 5, 2, 6, 1, 3}。
    int[] array = new int[max];
    static int count = 0;
    static int judgeCount = 0;

    public static void main(String[] args) {
        // 测试一把,8皇后是否正确。
        Queue8 queue8 = new Queue8();
        queue8.check(0);
        System.out.printf("一共有%d种解法~~\n", count);
        System.out.printf("一共判断冲突的次数%d次~~", judgeCount);
    }

    // 写一个方法,可以将皇后摆放的位置输出。
    private void print() {
        count++;
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
        System.out.println();
    }

    // 判断当我们放置第n个皇后, 就去检测该皇后是否和前面已经摆放的皇后冲突。
    /**
     *
     * @param n 表示第n个皇后
     * @return
     */
    private boolean judge(int n) {
        judgeCount++;
        for (int i = 0; i < n; i++) {
            // 说明:
            //1. array[i] == array[n]  表示判断第n个皇后是否和前面的n-1个皇后在同一列。
            //2. Math.abs(n-i) == Math.abs(array[n] - array[i]) 表示判断第n个皇后是否和第i个皇后在同一斜线。
            //3. 判断是否在同一行, 没有必要,n 每次都在递增。
            if (array[n] == array[i] || Math.abs(n-i) == Math.abs(array[n] - array[i])) {
                return false;
            }
        }
        return true;
    }

    // 编写一个方法,放置第n个皇后。
    // 特别注意: check 是每一次递归时,进入到check中都有 for(int i = 0; i < max; i++),因此会有回溯。
    private void check(int n) {
        // n = 8, 这时8个皇后就已经放好。
        if (n == max) {
            print();
            return;
        }
        // 依次放入皇后,并判断是否冲突。
        for (int i = 0; i < max; i++) {
            // 先把当前这个皇后 n , 放到该行的第i列。
            array[n] = i;
            // 判断当放置第n个皇后到第i列时,是否冲突。
            if (judge(n)) { // 不冲突。
                check(n + 1);   // 如果不冲突, 接着放n+1个皇后, 即开始递归。
            }
        }
    }
}

运行结果

D:\java\jdk-11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2	
4	1	7	0	3	6	2	5	
4	2	0	5	7	1	3	6	
4	2	0	6	1	7	5	3	
4	2	7	3	6	0	5	1	
4	6	0	2	7	5	3	1	
4	6	0	3	1	7	5	2	
4	6	1	3	7	0	2	5	
4	6	1	5	2	0	3	7	
4	6	1	5	2	0	7	3	
4	6	3	0	2	7	5	1	
4	7	3	0	2	5	1	6	
4	7	3	0	6	1	5	2	
5	0	4	1	7	2	6	3	
5	1	6	0	2	4	7	3	
5	1	6	0	3	7	4	2	
5	2	0	6	4	7	1	3	
5	2	0	7	3	1	6	4	
5	2	0	7	4	1	3	6	
5	2	4	6	0	3	1	7	
5	2	4	7	0	3	1	6	
5	2	6	1	3	7	0	4	
5	2	6	1	7	4	0	3	
5	2	6	3	0	7	1	4	
5	3	0	4	7	1	6	2	
5	3	1	7	4	6	0	2	
5	3	6	0	2	4	1	7	
5	3	6	0	7	1	4	2	
5	7	1	3	0	6	4	2	
6	0	2	7	5	3	1	4	
6	1	3	0	7	4	2	5	
6	1	5	2	0	3	7	4	
6	2	0	5	7	4	1	3	
6	2	7	1	4	0	5	3	
6	3	1	4	7	0	2	5	
6	3	1	7	5	0	2	4	
6	4	2	0	5	7	1	3	
7	1	3	0	6	4	2	5	
7	1	4	2	0	6	3	5	
7	2	0	5	1	4	6	3	
7	3	0	2	5	1	6	4	
一共有92种解法~~
一共判断冲突的次数15720次~~
Process finished with exit code 0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘淘图兔兔呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值