Skip to content

8皇后问题

package datastructure;

import java.util.Arrays;

public class EQueen {

    int[] queens = new int[8];
    public boolean solve(int col){
        if(col == 8) {
            return true;
        }
        // i代表行
        for(int i = 0; i < 8; i++) {
            // try row[i]
            queens[col] = i;

            boolean flag = true;
            for(int j = 0; j < col; j++) {

                var rowDiff = Math.abs(queens[j] - i);
                var colDiff = col - j;

                if(queens[j] == i || rowDiff == colDiff) {
                    flag = false;
                    break;
                }
            }

            if(flag && solve(col + 1)) {
                return true;
            }
        }
        return false;
    }

    void print(){

        for(int i = 0; i < 8; i++) {

            for(int j = 0; j < 8; j++) {
                if(queens[i] == j) {
                    System.out.print("Q");
                } else {

                    System.out.print(".");
                }

            }
            System.out.print("\n");
        }
    }

    public static void main(String[] argv) {
        var equeen = new EQueen();
        equeen.solve(0);
        equeen.print();

    }

}

文章来源于自己总结和网络转载,内容如有任何问题,请大佬斧正!联系我