如何解决:数独求解器堆栈溢出问题

时间:2019-02-08 11:40:54

标签: java recursion stack-overflow backtracking sudoku

我试图用Java创建Sudoku求解器,通常来说,我是编程和Java的新手。我真的不知道如何处理这种错误。 我不断收到堆栈溢出错误。

我尝试过使用不同的代码,但是它们都不起作用,但是无论如何,这是我最新的代码:

public class Sudoku {

    private int[][] values;
    private boolean [][] writable;
    private static final int ZERO = 0;
    private static final int SIZE = 9;

    //just a normal constructor that sets which values are changeable and which aren't. only values equal to zero are changeable.
    public Sudoku(int[][] values) {
        this.values = new int[SIZE][SIZE];
        for(int row = 0; row< SIZE ; row++)
        {
            for(int col = 0; col< SIZE; col++)
            {
                this.values[row][col] = values[row][col];
            }
        }

         writable = new boolean[values.length][values[1].length];
         for(int i = 0;i < writable.length;i++)
         {
             for(int j = 0; j<writable[1].length;j++)
             {
                 if(values[i][j] == ZERO)
                 {
                     writable[i][j] = true;
                 }
             }
         }
    }

    public void setValues(int row,int col ,int value) //changes the value if the value was changeable.
    {
        if(writable[row][col])
        {
            values[row][col]= value;
        }

    }

    public int getValue(int row,int col) {
        return values[row][col];
    }

    public boolean isWritable(int row,int col)
    {
        return writable[row][col];
    }
    private boolean ConflictAtRow(int row , int num)
    {
        for(int i = 0;i < SIZE;i++)
            if(getValue(row,i) == num)
                return true;
            return false;
    }
    private boolean ConflictAtCol(int col, int num)
    {
        for(int i = 0;i<SIZE;i++)
            if(getValue(i,col) == num)
                return true;
            return false;
    }
    private boolean ConflictAtBox(int row, int col, int num)
    {
        int r = row - row %3;
        int c = col - col %3;

        for(int i = r;i<r+3;i++)
        {
            for(int j = c;j<c+3;j++)
            {
                if(getValue(i, j) == num && row != i && col != j)
                    return true;
            }
        }
        return false;
    }
    private boolean ConflictAt(int row, int col, int num)
    {
        return ConflictAtBox(row, col, num) && ConflictAtCol(col,num) && ConflictAtRow(row, num);  //line 108
    }

    public boolean solve(int row,int col) 
    {
        int nextRow = (col < 8) ? row:row+1;
        int nextCol = (col +1)%9;
        for (row = nextRow; row < SIZE; row++) {
            for (col = NextCol; col < SIZE; col++) {
                if(isWritable(row,col))
                {
                    for (int num = 1; num <= 9; num++) {
                        if(!ConflictAt(row,col,num)) //line 118
                        {
                            setValues(row,col,num);

                         if(solve(nextRow,nextCol)) //line 122
                             return true;
                         }
                             setValues(row,col,ZERO);

                    }
                }return !ConflictAt(row,col,getValue(row,col)) && 
                  solve(nextRow,nextCol);;

            }

        }return true;
    }

当我运行resolve()方法时,我得到了堆栈溢出错误

Exception in thread "main" java.lang.StackOverflowError
    at Sudoku.Sudoku.ConflictAt(Sudoku.java:108) 
    at Sudoku.Sudoku.solve(Sudoku.java:118) 
    at Sudoku.Sudoku.solve(Sudoku.java:122) 
    at Sudoku.Sudoku.solve(Sudoku.java:122) 
    at Sudoku.Sudoku.solve(Sudoku.java:122) 
    at Sudoku.Sudoku.solve(Sudoku.java:122) 

以此类推...

1 个答案:

答案 0 :(得分:0)

一旦控件首次进入solve()方法,并且直到第122行的所有if条件求和为true,您就在调用solve()再次使用方法。

问题在于,每次控件单击此方法时,就好像它是第一次执行该方法一样。因为条件没有变化(for循环总是从0开始)。

这意味着,solve()方法被反复调用,直到堆栈内存不足为止。