The Eight Queens Problem_Enumeration (Java)

The violent enumeration method solves the problem of eight queens. The key to the problem is how to record the position of the queens and how to judge whether they are placed as required.

1. First, we can use a one-bit array to indicate the position of the queen (for example, a[1] = 2 means there is a queen in the first column and second row), and update this one-dimensional array when enumerating various placement positions.

2. Secondly, how to judge whether they are properly placed, or to judge the value stored in the array once. First of all, we can be sure that each column can only have one queen, so we first judge whether their rows are duplicated a[i] ==a[j] means repetition, and then compare whether their oblique columns are repeated, abs(a[i]-a[j]) == i-j means repetition. When traversing in the method, remember to "j <= i-1" and not less than, just an equal sign, the result can be described as a world of difference, one has more than 1.8 million results, and one has 92 types. Of course the latter is the correct answer.


public class 八皇后问题_枚举 {
    
    
    //定义棋盘
    static int[][] qipan = new int[9][9];
    public static void main(String[] args) {
    
    
        Queen();
    }
    static void Queen(){
    
    
        //定义一个标记数组 a[1] = 2表示第一列第二行有一个皇后
        int[] a = new int[9];
        //定义计数器
        int count = 0;
        int i;
            for (a[1] = 1;a[1] < 9;a[1]++)
                for (a[2] = 1;a[2] < 9;a[2]++)
                    for (a[3] = 1;a[3] < 9;a[3]++)
                        for (a[4] = 1;a[4] < 9;a[4]++)
                            for (a[5] = 1;a[5] < 9;a[5]++)
                                for (a[6] = 1;a[6] < 9;a[6]++)
                                    for (a[7] = 1;a[7] < 9;a[7]++)
                                        for (a[8] = 1;a[8] < 9;a[8]++){
    
    
                                            if(!cover(a))
                                                continue;
                                            else{
    
    
                                                count++;
                                                qipan[a[1]][1] = 1;
                                                qipan[a[2]][2] = 1;
                                                qipan[a[3]][3] = 1;
                                                qipan[a[4]][4] = 1;
                                                qipan[a[5]][5] = 1;
                                                qipan[a[6]][6] = 1;
                                                qipan[a[7]][7] = 1;
                                                qipan[a[8]][8] = 1;
                                                for (i = 1;i < 9;i++){
    
    
                                                    for (int j = 1;j < 9;j++){
    
    
                                                        System.out.print(qipan[i][j]+" ");
                                                        qipan[i][j] = 0;
                                                    }
                                                    System.out.println();
                                                }
                                                System.out.println();
                                            }
                                    }
                                    System.out.println("一共有"+count+"种方法");
    }
    static boolean cover(int a[]){
    
    
        int i = 2;
        int j = 1;
        boolean res = true;

        for(i = 2;i < 9;i++){
    
    
            for(j = 1;j <= i-1;j++){
    
    
                if((a[i] == a[j]) || ((Math.abs(a[i]-a[j])) == i -j)){
    
    
                    res = false;
                    break;
                }
            }
            if(!res)
                break;
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/105537473