Giter Club home page Giter Club logo

sudoku-solver-csharp's Introduction

Sudoku Solver in C#

Another breakable toy, my variation of Sudoku solver. I've created it without previously googling the topic , and I was quite surprised when latter I realized that most solutions out there use just dumb trial and error. I was also glad that I can re-invent backtracking algorithm ;)

Ok. let's start, from having a Sudoku that we want to solve:

Example Sudoku

After a little consideration I decided to solve it by elimination of possible values. Let's consider first block (by block I mean inner 3x3 cells squares), possible values for empty cells:

Cell \ Value 1 3 6 7 8 9
A1 x x x x
A2 x x x x
A3 x x x x
B2 x x x
B3 x x x x
C3 x x x

We can see that value 1 can be only in A1 cell so fill it in. Check remaining cells and no single value in a row or column. So we evaluate next block:

Cell \ Value 1 2 3 4 6
A5 x x x
B3 x x x x
B4 x
B5 x x x x
C3 x x

Now value 1 and 6 are only possible in cells B5 and B4 so we fill them in. Check remaining cells after reduction - no single possible values - we go to the next block.

We cycle through all blocks until all cells are filled in (will happen only for very simple Sudoku) or number of empty cells is not changed after a cycle. In the second case, we filled what we could using simple elimination and it's time for more advanced solving techniques or guessing. This time I choose guessing, maybe later I'll implement something more original. So we take first empty cell pick its first possible value fill it in and use recursion and [backtrack algorithm] 1 to check if we can complete the puzzle. If not go back pick next possible value and try again.

That's it the whole working solution can be found on [github] 2. To test the solution I've used online solver at [Sudoku Wiki] 3. Beside of detail description of different techniques of solving Sudoku, it allows to import and export puzzle as string of numbers which is really helpful. Next step for me is to implement the same algorithm using less familiar languages. I think of Python, Ruby, JavaScript and Dart, F# and maybe Haskell.

sudoku-solver-csharp's People

Contributors

sakowiczm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.