Skip to content
Prev 83937 / 398500 Next

sudoku

I "replied all" to the original message, but since that was to 
"r-packages at stat.math.ethz.ch" it might not have gone out there, did it? 
    If not, below is my reply again. [You have restrict the 
randomization so that you permute within and between block rows/columns.]

/Henrik

-------- Original Message --------
Subject: Re: [R] [R-pkgs] sudoku
Date: Sat, 07 Jan 2006 09:36:54 +1100
From: Henrik Bengtsson <hb at maths.lth.se>
To: Brahm, David <David.Brahm at geodecapital.com>
CC: r-packages at stat.math.ethz.ch
References: 
<4DD6F8B8782D584FABF50BF3A32B03D801A2BCDB at MSGBOSCLF2WIN.DMN1.FMR.COM>
Brahm, David wrote:
> Any doubts about R's big-league status should be put to rest, now that
 > we have a
 > Sudoku Puzzle Solver.  Take that, SAS!  See package "sudoku" on CRAN.
 >
 > The package could really use a puzzle generator -- contributors are
 > welcome!

Last summer I put a quick generator together after discussing with some
friends how these games a generate (and enumerated).  I don't know if it
is a correct/complete generator, but consider an empty game with 3x3
grids each with 3x3 cells.  Create the initial board  by adding 1:9 in
the first row, the c(2:9,1), in the second and so on, to make sure you
have one correct board.  From this you can now generate all(?) other
possible boards by permuting rows and columns.  You can for instance use
a random seed enumerate all such boards.  Finally, you want to remove
some of cells, which you also can by sampling using known random seeds.

See attached code.  Example:

  > source("Sudoku.R")
  > Sudoku$generate()
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
   [1,]    1   NA    3    4   NA   NA   NA    8   NA
   [2,]    4   NA   NA   NA    8   NA   NA   NA   NA
   [3,]   NA    8   NA    1   NA    3    4    5    6
   [4,]    2   NA   NA   NA   NA   NA   NA   NA    1
   [5,]   NA    6    7   NA   NA   NA    2   NA    4
   [6,]    8   NA   NA   NA   NA   NA   NA    6   NA
   [7,]   NA   NA    5   NA   NA    8    9   NA   NA
   [8,]    6   NA    8    9    1   NA    3   NA   NA
   [9,]   NA    1   NA    3   NA   NA   NA    7    8
  > Sudoku$generate(1)
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
   [1,]    3   NA    2    5   NA   NA   NA    9   NA
   [2,]    9   NA   NA   NA    3   NA   NA   NA   NA
   [3,]   NA    4   NA    8   NA    7    2    3    1
   [4,]    2   NA   NA   NA   NA   NA   NA   NA    6
   [5,]   NA    3    4   NA   NA   NA    1   NA    9
   [6,]    8   NA   NA   NA   NA   NA   NA    5   NA
   [7,]   NA   NA    9   NA   NA    2    6   NA   NA
   [8,]    7   NA    6    9    1   NA    3   NA   NA
   [9,]   NA    2   NA    6   NA   NA   NA    1    8
  > Sudoku$generate(2)
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
   [1,]    7   NA    6    1   NA   NA   NA    2   NA
   [2,]    1   NA   NA   NA    3   NA   NA   NA   NA
   [3,]   NA    2   NA    7   NA    5    9    8    1
   [4,]    8   NA   NA   NA   NA   NA   NA   NA    5
   [5,]   NA    9    1   NA   NA   NA    7   NA    8
   [6,]    5   NA   NA   NA   NA   NA   NA    9   NA
   [7,]   NA   NA    5   NA   NA    7    2   NA   NA
   [8,]    9   NA    8    3    2   NA    5   NA   NA
   [9,]   NA    1   NA    6   NA   NA   NA    7    9

/Henrik

 > -- David Brahm (brahm at alum.mit.edu)
 >
 >
 > 	[[alternative HTML version deleted]]
 >
 > _______________________________________________
 > R-packages mailing list
 > R-packages at stat.math.ethz.ch
 > https://stat.ethz.ch/mailman/listinfo/r-packages
 >
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html
 >
 >
roger bos wrote:
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Sudoku.R
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20060110/70eba65f/Sudoku.pl