DATA MINING
Desktop Survival Guide by Graham Williams |
|||||
Sudoku |
Solve a sudoku puzzle with the sudoku
package! Into a
file place the following representation of a sudoku puzzle:
2--f-c-8----ab7- e---a-6-f-03--d- --305b-f--2496-- -7------b---5-f1 -------e9-d----6 -0fec-89-----1-b 6----532--7--9-8 -9--d--0----7a4- -6dc----3--7--a- 7-0--e--46b----9 1-e-----5d-9fc3- 4----3-ae------- 93-1---c------e- --72e4--8-6dbf-- -e--8a-3-1-5---c -a45----7-9-8--0 |
> install.packages("sudoku") > library(sudoku) > library(help=sudoku) > sud <- readSudoku("sudoku.txt") > solveSudoku("sudoku.txt", map=c(0:9,letters)) 2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 2 4 6 f 0 c e 8 d 9 5 1 a [2,] e b 5 9 a 1 6 7 f 8 0 3 c [3,] c 1 3 0 5 b d f a 7 2 4 9 [4,] 8 7 a d 3 9 2 4 b e c 6 5 [5,] 5 8 1 7 4 f a e 9 b d 0 3 [6,] a 0 f e c 7 8 9 6 4 3 2 d [7,] 6 d c 4 b 5 3 2 1 a 7 f e [8,] 3 9 2 b d 6 1 0 c 5 e 8 7 [9,] b 6 d c 9 8 4 1 3 2 f 7 0 [10,] 7 5 0 3 f e c d 4 6 b a 1 [11,] 1 2 e a 7 0 b 6 5 d 8 9 f [12,] 4 f 9 8 2 3 5 a e 0 1 c 6 [13,] 9 3 8 1 6 d 0 c 2 f a b 4 [14,] 0 c 7 2 e 4 9 5 8 3 6 d b [15,] f e b 6 8 a 7 3 0 1 4 5 2 [16,] d a 4 5 1 2 f b 7 c 9 e 8 [,14] [,15] [,16] [1,] b 7 3 [2,] 4 d 2 [3,] 6 8 e [4,] 0 f 1 [5,] 2 c 6 [6,] 1 5 b [7,] 9 0 8 [8,] a 4 f [9,] e a 5 [10,] 8 2 9 [11,] c 3 4 [12,] 7 b d [13,] 5 e 7 [14,] f 1 a [15,] d 9 c [16,] 3 6 0 |
Another one. The original pattern is:
+-------+-------+-------+ | 1 . . | . . 7 | . 9 . | | . 3 . | . 2 . | . . 8 | | . . 9 | 6 . . | 5 . . | +-------+-------+-------+ | . . 5 | 3 . . | 9 . . | | . 1 . | . 8 . | . . 2 | | 6 . . | . . 4 | . . . | +-------+-------+-------+ | 3 . . | . . . | . 1 . | | . 4 . | . . . | . . 7 | | . . 7 | . . . | 3 . . | +-------+-------+-------+ |
As a text file for R:
1----7-9- -3--2---8 --96--5-- --53--9-- -1--8---2 6----4--- 3------1- -4------7 --7---3-- |
And the solution!
> z <- readSudoku("sudoku2.txt") > z [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 0 0 0 0 7 0 9 0 [2,] 0 3 0 0 2 0 0 0 8 [3,] 0 0 9 6 0 0 5 0 0 [4,] 0 0 5 3 0 0 9 0 0 [5,] 0 1 0 0 8 0 0 0 2 [6,] 6 0 0 0 0 4 0 0 0 [7,] 3 0 0 0 0 0 0 1 0 [8,] 0 4 0 0 0 0 0 0 7 [9,] 0 0 7 0 0 0 3 0 0 > solveSudoku(z) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 6 2 8 5 7 4 9 3 [2,] 5 3 4 1 2 9 6 7 8 [3,] 7 8 9 6 4 3 5 2 1 [4,] 4 7 5 3 1 2 9 8 6 [5,] 9 1 3 5 8 6 7 4 2 [6,] 6 2 8 7 9 4 1 3 5 [7,] 3 5 6 4 7 8 2 1 9 [8,] 2 4 1 9 3 5 8 6 7 [9,] 8 9 7 2 6 1 3 5 4 |