java - 8 非攻击皇后算法与递归

标签 java recursion n-queens

我在编写 8 皇后问题时遇到问题。我编写了一个类来帮助我解决它,但由于某种原因,我做错了什么。我有点明白应该发生什么。

此外,我们必须使用递归来解决它,但我不知道如何使用我读过的回溯,所以我只是在检查位置是否合法的方法中使用它。

我的棋盘是 String [] [] board = { { "O", "O"... 等等,有 8 行和 8 列。 如果我在概念上有任何错误或犯了严重的 Java 错误,请这样说 :D 谢谢!

public void solve () {
    int Queens = NUM_Queens - 1;
    while (Queens > 0) {
        for (int col = 0; col < 8; col++) {
            int row = -1;
            boolean c = false;
            while (c  = false && row < 8) {
                row ++;
                c = checkPos (row, col);
            }
            if (c == true) {
                board[row][col] = "Q";
                Queens--;
            }
            else 
                System.out.println("Error");
        }
    }
    printBoard ();
}

// printing the board
public void printBoard () {
    String ret = "";
    for (int i = 0; i < 8; i++) {
        for (int a = 0; a < 8; a++)
            ret += (board[i][a] + ", ");
        ret += ("\n");
    }
    System.out.print (ret);
}

// checking if a position is a legitimate location to put a Queen
public boolean checkPos (int y, int x) {
    boolean r = true, d = true, u = true, co = true;
    r = checkPosR (y, 0);
    co = checkPosC (0, x);
    int col = x;
    int row = y;
    while (row != 0 && col != 0 ) { //setting up to check diagonally downwards
        row--;
        col--;
    }
    d = checkPosDD (row, col);
    col = x;
    row = y;
    while (row != 7 && col != 0 ) { //setting up to check diagonally upwards
        row++;
        col--;
    }
    d = checkPosDU (row, col);
    if (r = true && d = true && u = true && co = true)
        return true;
    else
        return false;
}

// checking the row
public boolean checkPosR (int y, int x) {
    if (board[y][x].contentEquals("Q"))
            return false;
    else if (board[y][x].contentEquals("O") && x == 7)
        return true;
    else //if (board[y][x].contentEquals("O"))  
        return checkPosR (y, x+1);
}

// checking the column
public boolean checkPosC (int y, int x) {
    if (board[y][x].contentEquals("Q"))
            return false;
    else if (board[y][x].contentEquals("O") && y == 7)
        return true;
    else //if (board[y][x].contentEquals("O"))  
        return checkPosR (y+1, x);
}

// checking the diagonals from top left to bottom right
public boolean checkPosDD (int y, int x) {
    if (board[y][x].contentEquals("Q"))
        return false;
    else if (board[y][x].contentEquals("O") && (x == 7 || y == 7))
        return true;
    else //if (board[y][x].contentEquals("O"))  
        return checkPosR (y+1, x+1);
}

 // checking the diagonals from bottom left to up right
public boolean checkPosDU (int y, int x) {
    if (board[y][x].contentEquals("Q"))
        return false;
    else if (board[y][x].contentEquals("O") && (x == 7 || y == 0))
        return true;
    else //if (board[y][x].contentEquals("O"))  
        return checkPosR (y-1, x+1);
    }
}

最佳答案

因为这是家庭作业,解决方案,但不是代码。

尝试编写一个方法,只处理需要在单个列上发生的事情;这是你应该使用递归的地方。通过检查是否存在解决方案来进行回溯,如果不存在,则撤消上次更改(即更改皇后位置)并重试。如果您只关注问题的一部分(一列),这比同时考虑所有列要容易得多。

正如 Quetzalcoatl 指出的那样,您在第一个循环中将 false 分配给您的变量。你可能不想那样做。您应该始终在编译器中启用所有警告(使用 -Xlint 运行 javac)并修复它们。

关于java - 8 非攻击皇后算法与递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15219216/

相关文章:

java - 在 Windows LAF 下为每个组件的 JTextField 设置非 Activity 背景颜色

java - 构建无限深度的 JTree

java - 使用 PoolingClientConnectionManager 时释放连接?

c++ - 在 C++ 中生成排列的递归问题

c - 如何找到递归函数调用自身的次数?

java - N Queens 所有解决方案,目前显示 1

java - 解释 "volatile"在java中的用法?

Python:递归生成器

scala - 为什么并行范围处理比基于 Future 的并行处理(N 皇后示例)花费更多时间?

java - 8皇后不使用回溯法