-2

I am doing a project in my programming class and I chose to do a sudoku solver. However I haven't done any algorithms before and found this quite a challenge. I have read some articles about solving sudoku, but I am struggling with the actual code-writing. Could anyone provide me with some help?

So my teacher insisted on me using double array (int[,] type) so I am trying to stick with it. The problem is I don't really get how to go back to try another thing or what I mean really how to backtrack. If anyone could help me with code (even in 3x3 sudoku), I would be really grateful.

Also just in case it's unclear what I want, I've uploaded explanatory pic enter image description here

Really unclear thing is how do I code the third step (circled red).

bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • it's really quite easy: you show us your code and we try to help you - but if you don't have any ... well you better use your fav. search machine (there will be plenty of sudoku solvers for any programming language out there) – Random Dev Apr 09 '15 at 17:48
  • In my opinion, I don't think you would never actually do step 3 in that picture because you would never get to step 2. Step 2 is an invalid candidate so you would never generate or consider it (well...you would consider it...but only as an invalid candidate :p). Anyways backtracking can be done in several ways, but the simplest ways involve recursion, stacks, or queues. – Shashank Apr 09 '15 at 17:48
  • To help you a bit with backtracking: the easiest way to do it is to really deep-copy your array in a recursive algorithm because you don't have to revers actions on your game state – Random Dev Apr 09 '15 at 17:49
  • Why don't you make use of a constraint (logic) programming solver. For such solvers sudoku problems are literally peanuts. They solve it in less than 1 ms. – Willem Van Onsem Apr 09 '15 at 17:51
  • 1
    @CommuSoft ;) ... as I understand it is a project in school ... and I doubt that this is allowed - aside from this it will hurt nobody to reinvent a wheel from time to time - you will know a lot more about wheels after :D – Random Dev Apr 09 '15 at 17:58
  • @ThomasBrezinskas: do you mean with `3x3` that there are 81 squares? It's a bit unclear since you also have a 16 squares version. – Willem Van Onsem Apr 09 '15 at 17:58
  • @CarstenKönig: personally I think that's a bit problematic that they teach people to implement backtracking algorithms while AC-1 and AC-2 algorithm in constraint programming will in general nearly always outperform a simple backtracking algorithm. Teaching the backtracking approach of course useful. But you can do this in an environment where you focus more on how you can use different strategies (like domain splitting). – Willem Van Onsem Apr 09 '15 at 18:00
  • 1
    @CommuSoft I am a math tutor, and I can't tell you how many kids do exponents without truly understanding the meaning of multiplication. Maybe if we didn't take so many shortcuts....+1 Carsten – Matthew Frontino Apr 09 '15 at 18:06

2 Answers2

1

Simple Sudoku puzzles can be solved without any recursion. You go through each empty block and try to solve it. If it is the only block in

  1. The row
  2. or the column
  3. or the block

Then you enter the number. You do this until your riddle is solved.

Recursion kicks in!

However, if you stagnate and can't solve any more empty fields with this method, you have to go recursive. Pick the block that has the fewest possible options, for example a block where only 3 numbers are possible. Perform a recursion on those 3 numbers, trying each one. Then, try again by the method described above. When you stagnate again, make another step in recursion.

Many of your recursion cases will end up in a Sudoku riddle that is wrong (i.e. not solvable). You just ignore them. One of these cases will solve it.

Pseudo code

I'll try to mediate the idea here, not just paste a bunch of code. You should be able to solve this. It's a brain teaser, but possible

function Solve(grid)
    while
        SolveByLogic(grid)
        
        if (grid is not solved)
            cell = find cell with the least possible options
            for each of this option
                try to => Solve(grid)
            next
        end
    wend
end

function SolveByLogic(grid)
    // Try to solve it by pure logic
    while
        foundAny = false
        for each empty cell
            if (cell is the only empty cell in row)
                cell = what's left in that row
                foundAny = true
            else if (cell is the only empty cell in column)
                cell = what's left in that column
                foundAny = true
            else if (cell is the only empty cell in grid)
                cell = what's left in that grid (by grid I mean the 3x3 grid)
                foundAny = true
        next
        if (foundAny == false) return //No more logic solving possible
    wend
end

Note: This is not a complete algorithm that you can just convert into your language and use! But it should give you the idea that you need in order to solve this!

Good luck :)

Community
  • 1
  • 1
bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • I actually have made a project of this and put it on my website, including code: https://bytecode77.com/coding/projects/sudokucheater - if you are still interested. – bytecode77 Apr 14 '16 at 17:15
0

I would give the Dancing Links Algorithm a try, though it really works better with a custom Node object than with a rectangular array.

http://www-cs-faculty.stanford.edu/~uno/papers/dancing-color.ps.gz

The problem type is exact cover. Research different algorithms that can solve that type of problem and choose the one that works best for the parameters that you have.

maniak1982
  • 707
  • 2
  • 7
  • 23