solveSudoku: Solve a Sudoku Puzzle

View source: R/solveSudoku.R

solveSudokuR Documentation

Solve a Sudoku Puzzle

Description

Solves a Sudoku Puzzle.

Usage

  solveSudoku(z, verbose=FALSE, map=c(1:9,letters), level=0,
              print.it=TRUE)

Arguments

z

A filename (passed to readSudoku), or a numeric matrix.

verbose

If TRUE, report on progress.

map

Vector of unique puzzle elements (possibly longer than necessary). The default is c(1:9, letters), so an N=16 puzzle should be encoded using '1'-'9' and 'a'-'g'.

level

Recursion level (should not be set by user).

print.it

Logical: print the solution?

Details

A Sudoku puzzle consists of an NxN grid, where N is a perfect square (usually N=9). The grid is subdivided into N [sqrt(N) x sqrt(N)] boxes. You must fill in the missing values so that each row, each column, and each box contains the integers 1:N exactly once.

The algorithm uses an NxNxN array of logicals, representing the NxN cells and the N possible elements. For example, if a[1,2,3]=TRUE, then z[1,2] is known to be '3'. If a[1,2,4]=FALSE, then z[1,2] is known not to be '4'. The basic rules of Sudoku are used to fill in FALSE's, then elimination is used to find the TRUE's. If that approach runs out of steam, a guess is made and the program recurses to find either a solution or an inconsistency. No attempt is made to prove a solution's uniqueness.

Value

Invisibly returns the solved (numerical) matrix, and prints the character version.

Author(s)

David E. Brahm <brahm@alum.mit.edu>

References

Example "puz1" comes from https://sudoku.com/.

Examples

## Not run: 
  solveSudoku(system.file("puz1.txt",package="sudoku"), verbose=TRUE)

## End(Not run)

sudoku documentation built on April 19, 2022, 5:09 p.m.

Related to solveSudoku in sudoku...