I get an error due to an error that I understand, but I donβt understand why the stack overflow occurs in the first procedure in a recursive procedure, and not when the recursive procedure is called.
In the sudoku puzzle solving method, there is a recursive segment here (bold text is a recursive call:
System.out.print(""); <= Qaru occurs here
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];
int[][] solution = solveSudokuGrid( new_grid );
if( solution != null ) return solution;
}
Error (note that this happens in the System.out.print () statement):
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)
I expect the stack to overflow when the SudokuGrid solution is called, and not in the print statement. Why?
source
share