编辑代码

import java.util.LinkedList;
public class Main {
    public static void main(String[] args) {

        Queue queue=new Queue();
        queue.test();
    }
}
class Queue {
    private static boolean Correct(int col, int row, int count, int[] path) {
        boolean c = true;
        int left = col - 1;
        int right = col + 1;

        for (int i = row - 1; i >= 0; i--)
        {
            //判断上一行的皇后是否与当前位置同列
            if (path[i] == col) {
                c = false;
                break;
            }
            
            //判断上一行的皇后是否与当前位置左侧相邻
            if (left >= 0 && path[i] == left)
            {
                c = false;
                break;
            }

            //判断上一行的皇后是否与当前位置右侧相邻
            if ( right< count && path[i] == right)
            {
                c = false;
                break;
            }
            left--;
            right++;
        }

        return c;
    }
    public static void find(int queuecount, int Row, int[] path, LinkedList<int[]> Solution) {
        if (queuecount == Row) { //判断当前行数是否等于总行数
            Solution.add((int[])path.clone());
            return;
        }

        for (int a = 0; a < queuecount; ++a) {
            //判断每一列的当前位置是否合法
            if (Correct(a, Row, queuecount, path)) { 
                path[Row] = a;
                find(queuecount, Row + 1, path, Solution);
            }
        }
    }

    public static LinkedList<int[]> solve(int count) {
        LinkedList<int[]> Solution = new LinkedList<int[]>();
        int[] path = new int[count];

        find(count, 0,  path, Solution);
        return Solution;
    }

    public static void Printf(int Count, LinkedList<int[]> solution) {
        System.out.println("八皇后问题解决方案有:" + solution.size()+"种" );
        for (int[] solutions:solution) {
            for (int i = 0; i < solutions.length; ++i) { //外层循环遍历每一行
                for (int j = 0; j <Count; j++) { //内层循环遍历每一列,判断当前列是否与组solutions中的值相等
                    if (j == solutions[i])
                    {
                        System.out.print('1'); //相等,则打印字符'1'表示放置了皇后
                    }
                    else
                    {
                        System.out.print('0'); //不相等,则打印字符'0'表示没有放置皇后
                    }
                }
                System.out.println();
            }

            System.out.println();

        }
    }

    public  static void test() {
       
        LinkedList<int[]> solution = solve(4);
        Printf(4, solution);
     
    }
}