Fri, 29 Mar 2024

Puzzle Books Make Great Gifts for Family and Friends (or Yourself!)

Crossword a Day 2024
Crossword a Day 2024
Sudoku a Day 2024
Sudoku a Day 2024
Grandad's Crosswords
Grandad's Crosswords
Logic Puzzles for Adults
Logic Puzzles for Adults

How to Fill a Sudoku Grid

How do you actually go about filling a sudoku puzzle grid? This is a question we get asked from time to time, either by budding puzzle writers, those wanting to create a one-off puzzle, or, no doubt in some cases, those who have been tasked with creating a sudoku puzzle as a task as part of a programming course they are taking.


Filling the sudoku grid is fairly straightforward, and there are several different methods that can be used for doing this. Once you have a filled grid then going on to make a fair, valid and accurately graded puzzle is a lot harder. In this text and video discussion, we stick purely to how to perform the former task: filling the grid.


We can define the task as follows: create an algorithm that results in a 9x9 grid that is filled in such a way that the numbers from 1-9 appear once in each row, column and 3x3 box that compose the grid. Of course, other grid sizes exist, but 9x9 is by far the most common. And, in any case, the parameters plugged into the algorithm can be easily changed to reflect other grid sizes as required.


The video below walks through a simple and powerful algorithm that will do just this for you - quickly fill sudoku grids:





There are of course other ways to try and fill a grid. You can still fill standard 9x9 grids with an algorithm that doesn't backtrack at all, for instance by simply stopping each time you reach a point where there are no givens available for a certain square, and starting again from scratch. This will be quick enough to still fill grids in a reasonable time frame. However, the more constraints you add, the harder this gets: for instance if you add a diagonal constraint (sudoku X) then this approach can pretty much grid to a halt, so for maximum flexibility some form of backtracking is usually required to fill grids in a reasonable timeframe, as outlined in the video above.


Another method you can use to increase the chances of each attempt at a fill being successful is to actually implement solve rules that you'd use as a human when trying to tackle a puzzle, and try to make as much progress at each stage of the solve as you can. Whereas the video simply picks a square with the least options at each stage of the solve based on scanning the regions, you could implement solve rules to note that, for instance, where you have 45 as the only options for two squares in a region then none of the other squares in the region can contain either 4 or 5 so there is no point in trying them for those squares as a naive algorithm might do. Or, more simply, just write some code to observe if 9 is a candidate, say, for just one square in a region, then your program would know that square must be the 9 so even if it has several other candidates there is no need to try any of those options, and just go straight to the 9.


By thinking up, and experimenting with, different ways of trying to fill a grid, then you'll find a balance that works for you. Some methods may be more likely to lead to a grid fill but are actually slower overall than simpler methods, whilst some extremely quick and simple methods may have a low success rate. And, of course, there can be many ways of implementing the same algorithm, so the way you go about actually coding your chosen algorithm in the programming language of your choice will also have a big impact on the speed of fill.


Overall, you will probably find the algorithm outlined in the video strikes a good balance between simplicity of coding and speed at grid-filling. However if you use a different method to fill your sudoku grids then please do feel free to discuss in the comments below - it's always interesting to hear different methodologies for filling grids! So scroll to the bottom of the page and fill in the comments box if you'd like to share your own thoughts or experiences on filling sudoku grids.
Date written: 30 May 2020

Category: sudoku | Keywords: sudoku | programming | algorithms


Puzzle Videos: Learn to Solve

Consecutive Sudoku Rules
The rules of this unusual sudoku variant are explained in this video - they can be really fun to solve but you need to understand what the bars between squares mean and that all are shown...



Not tried consecutive sudoku before but like to give it a go? You can play the puzzle featured in the video via this link: Play Consecutive Sudoku Online


Read about other Puzzles




Comment on this blog post

If you would like to comment on this post, please enter your comments below; all fields are mandatory. Posts are moderated before display.
Your name:


Your comments:


Human testing question:
Is our favourite word: obscurity, clarity or semi-transparent?



Back to World of Puzzles
Puzzle Solutions
i Newspaper Solutions
Children's Puzzle Solutions

Key Links for Publishers

View a single page overview
List of puzzles
with a picture of each and a link to more information.

List of kids puzzles

Buy now
View content such as dot to dots, adult colouring and more available to license for print right now

Contact us


Word Puzzles
Buy Crosswords
Children's Puzzles
Wordsearch
CodeWords
Arrow words
Kriss Kross
Word Ladder
Word Wheel

Number & Colouring Puzzles
Buy Sudoku
Children's Sudoku
Samurai Sudoku
Jigsaw Sudoku
Futoshiki
Mind Games
Killer Sudoku
Kakuro
Chess Puzzles
A - Z of Puzzles
Colour By Number
Dot To Dot
Adult Colouring

Puzzle Tools
Puzzle FAQ
Codeword Solver
Rhyming Dictionary
Puzzle Solving Tools
Solving Videos

Services
Puzzles for Apps
Article Writing
Themed Puzzle Pages
Press Ready Puzzles
Questions
Page Fillers

View all services
© Clarity Media Ltd., 2024, company number 5465221 | Contact Us | Privacy Policy