我遇到一个堆栈溢出错误,我知道这是由于一个bug引起的,但我不明白为什么第一个递归过程中会发生堆栈溢出,而不是在调用递归过程时发生。
在解决数独谜题的方法中,以下是递归代码段(黑体字是递归调用):
堆栈溢出错误如下(请注意,它发生在System.out.print()语句上):
在解决数独谜题的方法中,以下是递归代码段(黑体字是递归调用):
System.out.print(""); <b><= stack overflow occurs here</b>
int[] move_quality_sorted_keys = Sorting_jsc.RadixSort_unsigned_1( move_quality );
for( int xPossibleMove = 1; xPossibleMove <= ctPossibleMoves; xPossibleMove++ ){
int xMove = move_quality_sorted_keys[ctPossibleMoves - xPossibleMove + 1];
int[][] new_grid = new int[10][10];
for( int xRow = 1; xRow <= 9; xRow++ )
for( int xColumn = 1; xColumn <= 9; xColumn++ )
new_grid[xRow][xColumn] = grid[xRow][xColumn];
new_grid[move_row[xMove]][move_column[xMove]] = move_value[xMove];
<b>int[][] solution = solveSudokuGrid( new_grid );</b>
if( solution != null ) return solution;
}
堆栈溢出错误如下(请注意,它发生在System.out.print()语句上):
Exception in thread "main" java.lang.StackOverflowError
at java.io.BufferedWriter.write(BufferedWriter.java:221)
at java.io.Writer.write(Writer.java:157)
at java.io.PrintStream.write(PrintStream.java:525)
at java.io.PrintStream.print(PrintStream.java:669)
at Euler100.solveSudokuGrid(Euler100.java:2458)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
at Euler100.solveSudokuGrid(Euler100.java:2467)
我会预计堆栈溢出会发生在调用solveSudokuGrid时,而不是print语句上。这是为什么呢?