编程练习:N皇后问题 (JAVA)

N-Queens

基本解法,使用二维数组,for循环两次,效率低

import java.util.*;

public class Queens {

    private int chessBoard[][];
    private int N;

    Queens(int N) {
        this.N = N;
        this.chessBoard = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                this.chessBoard[i][j] = 0;
            }
        }
    }

    private boolean is_attacked(int r, int c) {
        for (int i = 0; i < N; i++) {
            if (this.chessBoard[r][i] == 1) return true;
            if (this.chessBoard[i][c] == 1) return true;
            for (int j = 0; j < N; j++) {
                if (i + j == r + c && this.chessBoard[i][j] == 1) return true;
                else if (i - j == r - c && this.chessBoard[i][j] == 1) return true;
            }
        }
        return false;
    }

    public boolean N_Queen(int queens_left) {
        if (queens_left == 0) return true;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (!this.is_attacked(i, j)) {
                    this.chessBoard[i][j] = 1;
                    if (N_Queen(queens_left - 1)) {
                        return true;
                    } else {
                        this.chessBoard[i][j] = 0;
                    }
                }
            }
        }
        return false;
    }

    public void printChessBoard() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(this.chessBoard[i][j]+" ");
            }
            System.out.print("\n");
        }
    }

    public static void main(String args[]) throws Exception {

        Scanner s = new Scanner(System.in);
        int N = Integer.valueOf(s.nextLine());
        Queens queen = new Queens(N);
        if(queen.N_Queen(N)) {
            System.out.println("YES");
            queen.printChessBoard();
        }
        else {
            System.out.println("NO");
        }

    }
}

改进后使用一维数组存储棋盘信息,数组下标表示列,对应元素表示行。

import java.util.*;

public class Queens {

    private int chessBoard[];
    private int N;

    Queens(int N) {
        this.N = N;
        this.chessBoard = new int[N];
    }

    private boolean is_attacked(int c) {
        for (int i = 0; i < c; i++) {
            if (this.chessBoard[i] == this.chessBoard[c]) return true;
            if (this.chessBoard[i] + i == this.chessBoard[c] + c) return true;
            if (this.chessBoard[i] - i == this.chessBoard[c] - c) return true;
        }
        return false;
    }

    public boolean N_Queen(int x) {
        if (x == N) return true;
        for (int i = 0; i < N; i++) {
            this.chessBoard[x] = i;
            if (!this.is_attacked(x)) {
                if(N_Queen(x + 1)){
                    return true;
                }
            }
        }
        return false;
    }

    public void printChessBoard() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (i == this.chessBoard[j]) {
                    System.out.print("1 ");
                } else {
                    System.out.print("0 ");
                }
            }
            System.out.print("\n");
        }
    }

    public static void main(String args[]) throws Exception {

        Scanner s = new Scanner(System.in);
        int N = Integer.valueOf(s.nextLine());
        Queens queen = new Queens(N);
        if (queen.N_Queen(0)) {
            System.out.println("YES");
            queen.printChessBoard();
        } else {
            System.out.println("NO");
        }

    }
}

输入输出结果

经典的八皇后问题

public class Queens8 {
    private int chessBoard[];//数组下标表示列c,内存元素表示行r
    private int N;
    private int solution;

    Queens8(int N) {
        this.chessBoard = new int[N];
        this.N = N;
        this.solution = 0;

    }

    private boolean is_attacked(int c) {
        for (int i = 0; i < c; i++) {
            if (this.chessBoard[i] == this.chessBoard[c]) return true;
            if (this.chessBoard[i] + i == this.chessBoard[c] + c) return true;
            if (this.chessBoard[i] - i == this.chessBoard[c] - c) return true;
        }
        return false;
    }

    public void N_Queen(int x) {
        if (x == N) {
            solution++;
            return;
        }
        for (int i = 0; i < N; i++) {
            this.chessBoard[x] = i;
            if (!this.is_attacked(x)) {
                N_Queen(x + 1);
            }
        }
    }

    public static void main(String args[]) throws Exception {
        Queens8 queens = new Queens8(8);
        queens.N_Queen(0);
        System.out.print(queens.solution);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值