Eight Queens problem of data structures and algorithms

Eight Queens problem of data structures and algorithms


table of Contents

  1. Eight Queens problem introduced
  2. Eight queens problem analysis of thinking
  3. Code

1. Introduction eight queens problem

Eight queens problem is an old and well-known problems, is a typical case of backtracking algorithms. The problem is an international chess player Max Bethel to
put forward in 1848: placing eight queens on a chess 8 × 8 grid, it can not attack each other, namely: any two queens can not be in the same line ,
on the same row or the same slash and asked how many pendulum method (92)

Link: http://www.7k7k.com/swf/49842.htm

2. The eight queens problem analysis of thinking

  1. The first Queen's first put the first column of the first row
  2. The second Queen on the first column of the second row, and then determine whether OK, if not OK, continue to be on the second row, third row, followed by all the columns are done and find a suitable
  3. Queen continues the third, or the first column, a second column · "Until the first eight queens can not be placed in a position of conflict, it is found a correct solution
  4. When to get a correct solution when, at the time of the stack to fall back on a stack, will begin to backtrack, the upcoming first queen, put all the correct solution of the first column, all available.
  5. Then put back to continue with a second column Queen, continues to loop back to step 1,2,3,4
  6. schematic diagram:
    Here Insert Picture Description

Description: In theory should create a two-dimensional array to represent the board, but actually by the algorithm, using a one-dimensional array to solve the problem. arr [8] = {0,4,7,5,2,6,1,3} // arr corresponding to the subscript denotes the first few lines, i.e., the first of several Queen, arr [i]: val represents i + 1 queens, on the i + val + 1 column 1 of the first row


3. code implementation

package com.recursion;

public class Queue8 {

    //定义个max表示共有多少个皇后
    int max = 8;
    //定义数组array,保存皇后放置位置的结果,比如arr[8] = {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);

    }

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

            //如果冲突,继续执行 array[n] = i;即将第n个皇后,放置在本行后一个位置


        }


    }

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

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

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/92845533