Skip to content
Advertisement

Array from recursive call being overwritten

We’re making a program to solve an asterisk sudoku via a recursive approach with back tracking.

The solveIt method calls the solve method which is the recursive method. grid is declared before to be a 9×9 2D array that contains the puzzle to be filled in. If there is one solution, the program must print out the completed puzzle however if there are more solutions it must only print out the number of possible solutions.

Question is: Inside of solve, print(); works just fine and prints out the complete puzzle. However outside of the method it prints out the empty initial puzzle. Why is this? We cannot figure out why a separate variable (h in this case) also gets randomly overwritten when solve completes.

JavaScript

Advertisement

Answer

Solution

The .clone() method seems to simply reference h to grid. So h points to grid and takes on its values leading to the problem we were having above.

Therefore the following solution was implemented:

JavaScript

More information on clone():

https://www.geeksforgeeks.org/clone-method-in-java-2/

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement