I also tried to randomize the insertion of the numbers in the array. Actually, it seems to work, but not always: as more the numbers in the input matrix are, the probability of generating a infinite loop increases... maybe it's the algorithm?
Also, the final result is only shown in the output matrix of sudokuInsertIfPossible.vi, but in Main.vi I can see only the output matrix of the first call of solveSudoku.vi.
You mean you are Replacing Array Subset. Insert into Array is a completely different function that causes an array to grow.
I'm certain it would be the algorithm. Solving Sudoku puzzles is pretty complicated. It seems like it would require more code than you show here, but I can't see what is going on in the subVI's.
Would you really need recursion? I think it would be a case of continually iterating over the puzzle, trying to solve more blocks as you discover more answers.
I think this would be much more difficult without recursion. The algorithm relies on going back in time if the chosen number doesn’t provide a solution.
Since I couldn’t open the project, I created my own version of it, following the C code provided in the link. It does in fact work. I’ll post the code when I’m back at my computer.
I visualize an automated solver that assumes all values are possible in each open box. Then it eliminates possibilities because of values already known in the other boxes in the row, column, or 9-box. You iterate over everything. Once you see all choices have been eliminated but one, now you know the answer for that box and repeat the process. Basically what I do when solving them.
No recursion. No going back in time. Just a lot of iteration.
Then there are some other rules to apply like if you have two numbers that can only exist in two boxes. (So a 1-2 in one, and a 1-2 in another, then you eliminate all other possible numbers that could go into those same boxes. That would be a little harder to code for.
But what if a sudoku can have more than one solution?
Thanks aputman, I'll wait.
Have you ever seen one with more than 1 solution? It is certainly possible, but the puzzle makers are supposed to design it so it doesn't.
I think the code would just display the first one that is found. You would probably never know there was a second solution unless you changed the code to start from the end of the array rather than the beginning, or counted down from 9 to 1 instead of counting up from 1 to 9.
It appears that you are very close with your code. You still haven't removed the init matrix from the SolveSudoku VI. It will not work until you do this.
See attached project.
It's just a control reference with Array class selected. You can create one in a couple of different ways.