solveSudoku | R Documentation |
Solves a Sudoku Puzzle.
solveSudoku(z, verbose=FALSE, map=c(1:9,letters), level=0, print.it=TRUE)
z |
A filename (passed to |
verbose |
If TRUE, report on progress. |
map |
Vector of unique puzzle elements (possibly longer than
necessary). The default is |
level |
Recursion level (should not be set by user). |
print.it |
Logical: print the solution? |
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.
Invisibly returns the solved (numerical) matrix, and prints the character version.
David E. Brahm <brahm@alum.mit.edu>
Example "puz1" comes from https://sudoku.com/.
## Not run: solveSudoku(system.file("puz1.txt",package="sudoku"), verbose=TRUE) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.