Brute force sudoku solver python

A complete guide to solving a Sudoku puzzle with a recursive method

Brute force sudoku solver python

Photo by Andrey Metelev on Unsplash.

Sudoku is a popular Japanese puzzle game that is based on the logical placement of numbers. It doesn’t require any special mathematics skills or calculations. Let’s look at an example below from Wikipedia:

Brute force sudoku solver python

Source: Wikipedia

It is a 9x9 grid puzzle (81 squares). At the beginning of the game, some squares are filled with digits, while most of them are empty. The goal of Sudoku is to fill those empty squares with digits so that each row, column, and 3×3 section contains numbers between 1 to 9. The players need to use logic to fill in the missing digits and complete the grid so that all the constraints and rules are satisfied. A move is incorrect if:

  • Any row contains more than one of the same digit from 1 to 9.
  • Any column contains more than one of the same digit from 1 to 9.
  • Any 3×3 grid contains more than one of the same digit from 1 to 9.

Sudoku Solver (Brute Force Approach)

In this article, I will share how to solve the Sudoku puzzle above using a brute force approach.

First of all, let’s represent the puzzle as an 81-digit string:

Brute force sudoku solver python

Conversion of Sudoku puzzle to an 81-digit string. Image by the author.

Afterward, we can create the Sudoku Solver by following this three-step recursive procedure:

  1. Find the location of the first 0, denoted by i (i=0 means the first number, i=1 means the second, …, and i=80 means the last one .) If no 0 is found, a solution is found.
  2. For location i, scan elements in the same row, same column, and same block. This step is to find all the digits for which location i cannot be filled.
  3. Let location i try every possible value, one by one, and for each choice, repeat these three steps (until no 0 is found in step 1). Every possible value = digit from 1 to 9 excluding those digits for which location i cannot be filled.

This brute force approach uses a recursive function that is similar to using an 81-level nested for loop.

Step-by-Step Guide

Now let’s see how to implement it using code.

1. Find the location of the first 0

2a. How to tell if element i and element j are in the same row

Brute force sudoku solver python

On Left: Denoted the location 0–80 in the Sudoku puzzle, On Right: Output from the above code snippet. Image by Author

From this code snippet, we can see that i//9=0 for i in range(0, 9), i//9=1 for i in range(9, 18), …, and i//9=8 for i in range(72, 81).

2b. How to tell if element i and element j are in the same column

Brute force sudoku solver python

On Left: Denoted the location 0–80 in the Sudoku puzzle, On Right: Output from the above code snippet. Image by Author

From this code snippet, we can see that i%9=0 for i in [0, 9, 18, 27, 36, 45, 54, 63, 72], i%9=1 for i in [1, 10, 19, 28, 37, 46, 55, 64, 73], …, and i%9=8 for i in [8, 17, 26, 35, 44, 53, 62, 71, 80].

2c. How to tell if element i and element j are in the same block

Brute force sudoku solver python

On Left: Denoted the location 0–80 in the Sudoku puzzle, On Right: code snippets. Image by Author

From these code snippets, we can see that if elements i and j are in the same block, they will be in the same three rows and same three columns. Now let’s combine everything in step 2 to find all the digits for which location i cannot be filled.

Brute force sudoku solver python

An example shows the location i = 2. Image by Author
same row = {3, 5, 7}, same column = {8}, same block = {3, 5, 6, 8, 9}

cannotuse = {3, 5, 6, 7, 8, 9}, every_possible_values = {0, 1, 2, 4}

Sudoku Solver Solution

Now let’s combine everything above and solve the Sudoku puzzle.

Brute force sudoku solver python

Image by the author.

I hope you learned something from this tutorial.

Can you brute force Sudoku?

Although it has been established that approximately 5.96 x 1126 final grids exist, a brute force algorithm can be a practical method to solve Sudoku puzzles. A brute force algorithm visits the empty cells in some order, filling in digits sequentially, or backtracking when the number is found to be not valid.

Can a 9 * 9 Sudoku have multiple solutions?

A Sudoku puzzle can have more than one solution, but in this case the kind of logical reasoning we described while discussing solving strategies may fall short.

How does Python solve Sudoku?

Solving Sudoku using Linear Programming in Python.
Step 1: Define the Linear Programming problem..
Step 2: Set the objective function..
Step 3: Define the decision variables..
Step 4: Set the constraints..
Step 5: Solve the Sudoku puzzle..
Step 6: Check if an optimal result is found..

What is the fastest someone has solved a Sudoku puzzle?

According to Guinness World Records, the fastest time to complete a “Very Easy” difficulty Sudoku puzzle was 1 minute 23.93 seconds. The record was set on May 20, 2006 by Thomas Snyder, an American Sudoku champion.