# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Re: Help with Sudoku solver conversion from C

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.

Message 11 of 20
(1,020 Views)

## Re: Help with Sudoku solver conversion from C

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.

Message 12 of 20
(997 Views)

## Re: Help with Sudoku solver conversion from C

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.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 13 of 20
(993 Views)

## Re: Help with Sudoku solver conversion from C

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.

Message 14 of 20
(990 Views)

## Re: Help with Sudoku solver conversion from C

But what if a sudoku can have more than one solution?

Thanks aputman, I'll wait.

Message 15 of 20
(972 Views)

## Re: Help with Sudoku solver conversion from C

@Snorlite wrote:

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.

Message 16 of 20
(969 Views)
Solution
Accepted by Snorlite

## Re: Help with Sudoku solver conversion from C

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.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 17 of 20
(966 Views)

## Re: Help with Sudoku solver conversion from C

Thank you very much, it does indeed work. I'll look at your code in detail.

Message 18 of 20
(944 Views)

## Re: Help with Sudoku solver conversion from C

Just a question, how did you create a reference to GridIn in SolveSudoku.vi?

Message 19 of 20
(925 Views)

## Re: Help with Sudoku solver conversion from C

It's just a control reference with Array class selected.  You can create one in a couple of different ways.

1. Right click the grid and Create -> Reference.  The right click this reference and create a control from it.
2. Insert a control reference from the refnum palette.  Right click -> Select Class -> Generic -> GObject -> Control -> Array.
aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 20 of 20
(922 Views)