Java 恩奎恩谜题,皇后不放?

Java 恩奎恩谜题,皇后不放?,java,recursion,Java,Recursion,我正在为NQueens问题编写一个递归程序,具有指定的电路板大小。我的问题是,女王没有被列入董事会,我不知道为什么。我已经试着找出这个程序,但我仍然没有看到我的问题。有什么建议吗 public class Main { public static boolean[][] board; public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(Sy

我正在为NQueens问题编写一个递归程序,具有指定的电路板大小。我的问题是,女王没有被列入董事会,我不知道为什么。我已经试着找出这个程序,但我仍然没有看到我的问题。有什么建议吗

public class Main {


public static boolean[][] board;

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

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter size of board");
    int n = scan.nextInt();
    board = new boolean[n][n];

    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {

            board[i][j] = false;
        }
    }

    NQueens(0);
    printBoard();
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {

            if (board[i][j] == true) {

                System.out.println((i + 1) + " " + (j + 1));

            }
        }
    }
}

static boolean NQueens(int col) {
    if (col >= board.length)
        return true;

    for (int i = 0; i < board.length; i++) {
        if (checkNotBlocked(col, i)) {
            board[col][i] = true;
            if (NQueens(col + 1))
                return true;

            board[col][i] = false;
        }
    }
    return false;
}

static boolean checkNotBlocked(int col, int row) {

    for (int i = 0; i < col; i++) {
        if (board[i][row] == true)
            return false;
    }

    for (int i = col; i >= 0; i--) {
        for (int j = row; j >= 0; j--) {
            if (board[i][j] == true)
                return false;
        }
    }
    for (int i = col; i >= 0; i--) {
        for (int j = row; j < board.length; j++) {

            if (board[i][j] == true)
                return false;
        }
    }

    return true;
}


static void printBoard() {
    int i;
    for (i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            if (board[i][j] == true) {
                System.out.print("Q\t");
            } else {
                System.out.print("_\t");
            }
        }
            System.out.println("\n");
    }
}
公共类主{
公共静态布尔[][]板;
公共静态void main(字符串[]args)引发FileNotFoundException{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入电路板尺寸”);
int n=scan.nextInt();
board=新布尔值[n][n];
对于(int i=0;i=电路板长度)
返回true;
对于(int i=0;i=0;i--){
对于(int j=row;j>=0;j--){
如果(板[i][j]==真)
返回false;
}
}
对于(int i=col;i>=0;i--){
对于(int j=行;j
您对对角线的检查不正确。您使用了嵌套循环,这意味着您正在检查整个矩形,而不仅仅是包含正方形的对角线。如果边界框内任何其他行上有皇后,您将拒绝放置下一个皇后。因为这将在放置第一个皇后后发生,你永远不会有第二个女王进入董事会

请注意逻辑与您所拥有的不同:

for (int i = col; i >= 0; i--) {
    for (int j = row; j >= 0; j--) {
        if (board[i][j] == true)
            return false;
    }
}
…它遍历从[col][row]到[0][0]的所有有序对…而不是只检查对角线:

for (int i = col, j = row;   // My apologies if this isn't legal Java;
     i >= 0 & j >= 0;        //   I'm out of practice.
     i--, j--) {             // i and j must decrement *together*.

    if (board[i][j])
            return false;
    }
}

对相反的对角线也要这样做。

你最好的选择是:用调试器一步一步地检查程序,看看它在运行时做了什么。这似乎不是真的。我明白了。我从来没有见过像这样使用“for”循环,即两个变量同时递增或递减。我对编程有点陌生:)谢谢你的帮助lp!等等!你能不能很快向我解释为什么我在递归函数的递归调用下需要“return true”?这部分:for(int I=0;I