Java递归数独解算器,递归函数未返回正确值

Java递归数独解算器,递归函数未返回正确值,java,recursion,backtracking,sudoku,solver,Java,Recursion,Backtracking,Sudoku,Solver,我正在使用数独解算器,无法正确返回/结束解算器功能。调用moveOn()函数中的show(),它显示完成的数独精细,但是solve返回false。我试图让solve在问题解决时返回true,在问题无法解决时返回null,但我不知道如何实现这一点 L是棋盘的长度(9 x 9数独游戏的L=9) getSquare(r,c)返回表示数独板的二维数组中的值 不同的检查功能检查值是否适合特定位置。他们不是问题所在 show()函数在控制台中打印出数组,使其看起来像一个合适的数独板 我还有一个isSolve

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

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

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

不同的检查功能检查值是否适合特定位置。他们不是问题所在

show()
函数在控制台中打印出数组,使其看起来像一个合适的数独板

我还有一个
isSolved()
函数来检查2D数组,如果它是一个有效的已求解数独,则返回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;
}
//此方法的唯一用途是在数独中的下一个位置调用findNum函数
公共void moveOn(int行,int列){
//如果前一个位置不是该行的最后一个位置,则移动到该行的下一个单元格。
//如果它是行中的最后一个位置,请移动到下一行的第一列
如果(列+1!=L){solve(行,列+1);}
如果(行+1!=L){solve(行+1,0);}
else{show();}
}
//此方法查找数独网格上特定位置的有效数字\
公共布尔解算(int行,int列){
如果(行>=L){return true;}
//传递任何不是空的数字
如果(getSquare(行,列)!=0){moveOn(行,列);}
否则{
//尝试查找该位置的有效号码

对于(int n=1;n,在
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
}
在“solve”中使用:

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;
}
public boolean solve(int行,int列){
//传递任何不是空的数字
if(getSquare(row,column)!=0){return moveOn(row,column);}
否则{
//尝试查找该位置的有效号码

对于(int n=1;n,在
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
}
在“solve”中使用:

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;
}
public boolean solve(int行,int列){
//传递任何不是空的数字
if(getSquare(row,column)!=0){return moveOn(row,column);}
否则{
//尝试查找该位置的有效号码
对于(int n=1;n
此代码有效。。。
导入java.io.*;
导入java.util.array;
导入java.util.Scanner;
公共级数独{
公共静态int suRows=9;
公共静态int suColumns=9;
公共静态int[]suArray=新int[suRows][suColumns];
公共静态int[]dummyRowArray=新int[suRows];
公共静态int[]dummyColArray=新int[suColumns];
公共静态int[]dummy3x3Array=新int[3][3];
公共静态int[]dummyArray=new int[9];
//将文件内容读入二维数组
public static void readSudoku()抛出FileNotFoundException、IOException
{  
System.out.print(“请输入包含数独谜题的完整文件名(例如:X:/Sudoku\u case1.txt):”;
扫描仪扫描=新扫描仪(System.in);
字符串文件名=scan.nextLine();
文件=新文件(文件名);
if(file.exists())
{   
扫描仪内嵌=新扫描仪(文件);
对于(int i=0;i
此代码有效。。。
导入java.io.*;
导入java.util.array;
导入java.util.Scanner;
公共级数独{
公共静态int suRows=9;
公共静态int suColumns=9;
公共静态int[]suArray=新int[suRows][suColumns];
公共静态int[]dummyRowArray=新int[suRows];
公共静态int[]dummyColArray=新int[suColumns];
公共静态int[]dummy3x3Array=新int[3][3];
公共静态int[]dummyArray=new int[9];
//将文件内容读入二维数组
public static void readSudoku()抛出FileNotFoundException、IOException
{  
System.out.print(“请输入包含数独谜题的完整文件名(例如:X:/Sudoku\u case1.txt):”;
扫描仪扫描=新扫描仪(系统