5 - A simple Sudoku solver
Published online by Cambridge University Press: 05 November 2014
Summary
How to play: Fill in the grid so that every row, every column and every 3 × 3 box contains the digits 1–9. There's no maths involved. You solve the puzzle with reasoning and logic.
Advice on how to play Sudoku, the Independent
This chapter is devoted to an extended exercise in the use of lists to solve problems, and in the use of equational reasoning to reason about them and to improve efficiency.
The game of Sudoku is played on a 9 by 9 grid, though other sizes are also possible. Given a matrix, such as that in Figure 5.1, the idea is to fill in the empty cells with the digits 1 to 9 so that each row, column and 3 × 3 box contains the numbers 1 to 9. In general there may be any number of solutions, though in a good Sudoku puzzle there should always be a unique solution. Our aim is to construct a program to solve Sudoku puzzles. Specifically, we will define a function solve for computing a list of all the ways a given grid may be completed. If only one solution is wanted, then we can take the head of the list. Lazy evaluation means that only the first result will then be computed.
- Type
- Chapter
- Information
- Thinking Functionally with Haskell , pp. 89 - 109Publisher: Cambridge University PressPrint publication year: 2014