Java递归数独求解器,递归函数不返回正确值

标签 java recursion backtracking sudoku solver

我正在使用数独解算器,但无法正确返回/结束解算器功能。 moveOn() 函数中的 show() 被调用,它会很好地显示已完成的数独,但solve 返回 false。我试图在问题解决时让解决返回 true,在无法解决时返回 null,但不知道如何实现这一点。

L 是棋盘的长度(9 x 9 数独的 L = 9)

getSquare(r,c) 返回表示数独板的二维数组中的值

不同的检查函数检查某个值是否适合特定位置。它们不是问题。

show() 函数在控制台中打印出数组,因此它看起来像一个正确的数独板。

我还有一个 isSolved() 函数来检查二维数组,如果它是有效的已解决数独,则返回 true,否则返回 false。我也尝试将其实现到 solve() 方法中,希望使用它返回 true,但没有成功

//This method's only purpose it to call the findNum function on the next location in the sudoku
public void moveOn(int row, int column) {
    //if the previous location was not the last in the row move to ne next cell in said row.
    //if it was the last location in the row, move to the first column of the next row
    if (column + 1 != L) {solve(row, column + 1);}
    else if (row + 1 != L) {solve(row + 1, 0);}
    else {show();}
}

//This method finds a valid number for a specific location on the sudoku grid\
public boolean solve(int row, int column) {
    if (row >= L) {return true;}
    //pass over any numbers that are not empty
    if (getSquare(row, column) != 0) {moveOn(row, column);}
    else {
        //attempt to find a valid number for the location
        for (int n = 1; n <= L; n++) {
            if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
                // If a number is allowed at a specific location set that location to the number
                setSquare(row, column, n);
                //Begin checking for a solution based on previous numbers changed           
                moveOn(row, column);
            }               
        }
        //If no number is allowed in this space backtrack to the last successful number 
        //changed and reset all locations that have been changed recursively
        setSquare(row, column, 0);          
    }
    //If the puzzle is unsolveable
    return false;
}

非常感谢任何可以帮助阐明这一情况的人。 如果需要更多我的代码/信息,我很乐意提供

示例输入文件:http://pastebin.com/6mSKT3ES

编辑:删除完整代码

最佳答案

solve 函数中只有一个 return 语句,即

return false;

由于这是函数中的最后一个语句,并且是无条件执行的,因此 solve 将始终返回 false,除非引发异常。

要获得实际告诉您是否找到解决方案的返回值,您需要使返回值取决于条件。另外,一旦你找到了解决方案,对于适定谜题,继续搜索就没有意义了。

所以你应该在搜索循环中添加一个条件return true;。为此,您需要知道何时找到解决方案。您将递归包装在对 moveOn 的中间调用中,因此最简单的更改就是向 moveOn 添加返回值:

public boolean moveOn(int row, int column) {
    //if the previous location was not the last in the row move to ne next cell in said row.
    //if it was the last location in the row, move to the first column of the next row
    if (column + 1 != L) {return solve(row, column + 1);}
    else if (row + 1 != L) {return solve(row + 1, 0);}
    else {show(); return true;}  // reached end of grid, solved
}

并在“解决”中使用它:

public boolean solve(int row, int column) {
    //pass over any numbers that are not empty
    if (getSquare(row, column) != 0) {return moveOn(row, column);}
    else {
        //attempt to find a valid number for the location
        for (int n = 1; n <= L; n++) {
            if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
                // If a number is allowed at a specific location set that location to the number
                setSquare(row, column, n);
                //Begin checking for a solution based on previous numbers changed           
                if (moveOn(row, column)) {
                    return true;       // solved, yay!
                }
            }               
        }
        //If no number is allowed in this space backtrack to the last successful number 
        //changed and reset all locations that have been changed recursively
        setSquare(row, column, 0);          
    }
    //If the puzzle is unsolveable
    return false;
}

关于Java递归数独求解器,递归函数不返回正确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12964564/

相关文章:

java - 为什么信任 anchor 不应包含在 PKIX 认证路径中?

java - 如何交换两个按钮的背景颜色?

java - 用于更改 Android 应用布局背景颜色的切换按钮

java - addAction 总是将 actor 原点旋转 0,0,为什么?

javascript - 动态规划 : return all match data

java - 递归和循环哪个更有效?

java - 回溯 - 给定一组数字,找到和等于 M 的所有子集(M 已给定)

algorithm - 图上 DFS 的时间复杂度 (O(V+E)) 与矩阵上 DFS (3^(M*N))

Java:八皇后算法错误

javascript - 为什么汉诺塔中的 'disc' 值在 'disc' 次调用中不会递减为 0?