Sudoku Solver in 100 lines of code

This is an article on Sudoku Solver in 100 lines of code in Game Programming.

Rated 5.00 By 1 users

Hello

This is my first post on go4expert, and I hope it's done with the right foot .

The beggining

Although there are many solutions to solve sodoku on the Internet, after a discussion with some friends who work in the field, I inferred that there are two types of solutions specific to the sodoku game: the strategic ones, based on logical/human-style steps, and those that generate solutions based on a pattern.
The most easy to implement are those based on generation, and here two separate typical ways of implementation: greedy and backtracking.

So I decided to post a very simple solution, based on a primary implementation of a backtracking which is easy to understand, easy to implement and,the most interesting fact, has very few lines of code.

Advantages and disadvantages

The advantages of this implementation consists in the fact that, against the strategic methods, which are much faster than the generating algorithms, are rather difficult to implement, most often partial implementations rezulting in bugs for complex sudoku grids, the backtracking solution can be implemented in very few lines of code and can be characterized by both reliable and time efficient.

Even if sudoku is regarded as having only one solution, there are many cases in which sudoku grids have more solutions, either because the grid generating algorithms are defective or because this simple fact (the unique solution) is ignored by the application's provider. Considering this, human-style methods, although very fast, do not face very well sudoku grids with multiple solutions, and in this case, some of them rely on guessing technics or even forms of backtracking, against a simple backtracking that can find all solutions for sudoku grids with no problem at all.

However, the human-style methods can display the logical steps used, and they can show hints, which is very useful for customer applications who joins a sudoku player. This is one of the main drawbacks of this implementation, and all others of the same kind.

About the complexity of certain grids

Certain sites or applications offers a note of the complexity of certain sudoku grids. These degrees of complexity are valid only for human-style methods and should not be taken into consideration for backtracking algorithms. The complexity of a grid in case of backtracking should be determined by analysing the backtracking algorithm and lead to the worst-case-scenario.For example, for the algorithm below, the worst-case-scenario is a sudoku grid that has as posible candidates for the first cells big numbers like 9,8,7 and so on, increasing the backtracking's solving time. This can be easily overtaken by generating a random distribution between posible candidates on a speciefied cell. However, for simplicity, this is not done in the code below.

Differences between backtracking and greedy

May it be said, backtracking based solutions should not be considered simple generators, or brute-forcing algorithms, because this is the main difference between backtracking and greedy. Backtracking algorithms are looking for solutions using a well-known preset pattern, trying to optimize as much as posibile the search tree, against greedy who generates sudoku grids until it finds a valid solution. Backtracking is considered to be a quick-solver in opposition to the greedy, which is considered a purely brute-force algorithm.

The code

Code below is not fully optimized to remain as clear and eloquent as possible, being more useful than a beginner an experienced programmer. The main goal for this code, is to present a simple implementation, but without forgeting the speed and reliability that it should have.

The average time that takes for solving most of the grids is about 4-5 ms on a Athlon64 3000+@1800Mhz and about 2-4 ms on a Intel Core2Duo@2Ghz.

Code explanations

The solver is incapsulated within the class CSudokuSolver, and it has only two accesible functions Solve and GetSolution. The Solve function will initialize internal data by calling the Init function and it will run the core solving procedure by calling BTDo. The GetSolution function is intended to be called after Solve function, and will copy the solved grid into a bidim array. In case no solution is found or Solve wasn't called before, GetSolution will return FALSE, and it will leave the array intact.
As you can see, the code is intended to be as lite and easy as posible.