Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

SQL Sudoko Expand / Collapse
Author
Message
Posted Thursday, June 22, 2006 2:44 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 10, 2012 9:48 AM
Points: 27, Visits: 18
Phil Factor's solution that I mentioned earlier in the thread uses the same approach as the one Joe Celco mentions that he uses. It is, as he says, far neater. The Wikipedia entry on Sudoku is excellent. It does touch on the various computer approaches to constructing and solving the puzzle Sudoku in the Wikipedia

Editor: Simple-Talk
Post #289510
Posted Thursday, June 22, 2006 4:21 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 29, 2007 5:24 PM
Points: 20, Visits: 1

I noticed the same things too.

Creating the table sudoko was pretty trivial.

Of greater concern is the missing RemoveSolvedCells SP.

Also, since I am using MS SQL Server 2000, I had to change all the "sys.views," "sys.tables," "sys.procedures," etc. to sysobjects.

Some way to print out the unsolved and solved puzzle in a user readable format would be a nice addition.  Something that looks like this:

      START
 *-----------*
 |...|5.8|.47|
 |84.|...|..2|
 |...|64.|...|
 |---+---+---|
 |.6.|1..|57.|
 |..7|...|.16|
 |..4|.3.|.8.|
 |---+---+---|
 |73.|...|...|
 |..2|...|...|
 |...|9.4|1..|
 *-----------*

       SOLVED
 *-----------*
 |623|518|947|
 |841|397|652|
 |975|642|831|
 |---+---+---|
 |368|129|574|
 |297|485|316|
 |154|736|289|
 |---+---+---|
 |739|251|468|
 |412|863|795|
 |586|974|123|
 *-----------*

Post #289537
Posted Friday, June 23, 2006 3:34 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 21, 2008 3:30 PM
Points: 24, Visits: 6

I think I may have found a puzzle that this one doesn't solve. (Though it could just be that my implementation is wrong.)

 


x84|x6x|xxx
x9x|xxx|x7x
3xx|xx2|x5x
---+---+---
xxx|7xx|38x

xxx|2x5|xxx
x27|xx1|xxx
---+---+---
x1x|9xx|xx4
x3x|xxx|x1x
xxx|x1x|56x

 

I found this puzzle at www.websudoko.com

Post #289627
Posted Friday, June 23, 2006 5:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 23, 2007 11:05 AM
Points: 2, Visits: 1

Can I get someone that has gotten this to work upload their code or something so that I can verify it with my code?  My code is not working correctly.

 

Thanks

Post #289646
Posted Friday, June 23, 2006 6:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:42 AM
Points: 2,865, Visits: 1,705
My apologies for the missing stored procedure.

When I started building my solution I prototyped it by bouncing values through tables and the procedure was used to remove solved cells from a working table.

Of course once I worked out how to do the entire thing using views the procedure became redundant.



There is a form of Sudoko that I have seen called Ariadne's thread.

Ariadne is the person who supplied a Greek hero (Perseus?) with a thread so he could find his way around the Minotaur's labyrynth.

With this form of Sudoko you will reach a point where a decision has to be made between 2 values in a particular cell. Choose the wrong value and the puzzle won't solve, therefore you have to retrace your steps until you can fill in the alternative value.

Obviously, this T-SQL solution will not cater for this type of puzzle.


LinkedIn Profile
Newbie on www.simple-talk.com
Post #289660
Posted Friday, June 23, 2006 7:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 11, 2014 2:55 PM
Points: 11, Visits: 256
First off, cool article

I'm also using SQL 2000, so instead of using sys.views I use:

IF object_id('Digits') is not null drop view Digits

Here is a way to print the Sudoko table using SQL 2000. You need to change the options in query analyzer to not print column headers and set the output format to "space delimited".


select '*','-','-','-','-','-','-','-','-','-','-','-','*'

union all

select --rowId
'|'
,max(case columnId when 1 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 2 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 3 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
,max(case columnId when 4 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 5 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 6 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
,max(case columnId when 7 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 8 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 9 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
from sudoko
where rowId < 4
group by RowId

union all

select '|','-','-','-','+','-','-','-','+','-','-','-','|'

union all

select --rowId
'|'
,max(case columnId when 1 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 2 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 3 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
,max(case columnId when 4 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 5 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 6 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
,max(case columnId when 7 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 8 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 9 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
from sudoko
where rowId in (4,5,6)
group by RowId

union all

select '|','-','-','-','+','-','-','-','+','-','-','-','|'

union all

select --rowId
'|'
,max(case columnId when 1 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 2 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 3 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
,max(case columnId when 4 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 5 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 6 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
,max(case columnId when 7 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 8 then isnull(cast(cellValue as char(1)),'.') end)
,max(case columnId when 9 then isnull(cast(cellValue as char(1)),'.') end)
,'|'
from sudoko
where rowId > 6
group by RowId

union all

select '*','-','-','-','-','-','-','-','-','-','-','-','*'


Post #289707
Posted Friday, June 23, 2006 11:55 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, July 10, 2012 6:53 PM
Points: 46, Visits: 31

I learned a lot about T-SQL from this post!

Thank you very much!

Post #289821
Posted Saturday, June 24, 2006 6:53 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:42 AM
Points: 2,865, Visits: 1,705
Assuming you don't have SQL2005 and therefore the PIVOT function you could always resort to


DECLARE @display TABLE(ID INT IDENTITY(1,1),Display VARCHAR(50))
DECLARE @lRow TINYINT
SET @lRow = 0

DECLARE @NewRow VARCHAR(50)

INSERT INTO @display (Display) VALUES('|---+---+---|')

WHILE @lRow IS NOT NULL
BEGIN
SELECT @lRow = MIN(RowID) FROM dbo.sudoko WHERE RowId>@lRow

IF @lRow IS NOT NULL
BEGIN
SET @NewRow=''
SELECT @NewRow = COALESCE(@NewRow,'')+CAST(CellValue AS CHAR(1))
FROM dbo.sudoko
WHERE RowId=@lRow

SET @NewRow = LEFT(@NewRow,3)+'|'+SUBSTRING(@NewRow,4,3)+'|'+RIGHT(@NewRow,3)
INSERT INTO @display (Display) VALUES('|'+@NewRow+'|')

IF @lRow %3 = 0
INSERT INTO @display (Display) VALUES('|---+---+---|')

END
END

SELECT Display FROM @display


LinkedIn Profile
Newbie on www.simple-talk.com
Post #289921
Posted Saturday, June 24, 2006 1:43 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:23 PM
Points: 199, Visits: 590
Loved the idea, so tried to create my own. Which works except on Fridays and Saturdays in the Phoenix paper.

If somebody has scripted out and tried the authors solution would it work for the following suduko?

It chugs away nicely then stops producing new numbers, not sure if my algorythim (sp) is flawed or if the suduko puzzle requires a guess.

thanks
Daryl *dang its hot in phx*

create table #inputmysuduko (iv int, ir int, ic int)
insert into #inputMySuduko
select 6,1,2
union all select 5,1,4
union all select 2,1,8
union all select 4,1,9
union all select 1,2,4
union all select 6,2,6
union all select 3,2,8
union all select 5,2,9
union all select 2,3,5
union all select 7,3,7
union all select 7,4,1
union all select 5,4,2
union all select 9,4,3
union all select 6,4,4
union all select 2,6,6
union all select 1,6,7
union all select 5,6,8
union all select 7,6,9
union all select 5,7,3
union all select 9,7,5
union all select 4,8,1
union all select 9,8,2
union all select 2,8,4
union all select 5,8,6
union all select 1,9,1
union all select 3,9,2
union all select 4,9,6
union all select 8,9,8

Post #289931
Posted Sunday, June 25, 2006 5:09 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:42 AM
Points: 2,865, Visits: 1,705
Daryl, as posted earlier there are Sudoko puzzles where there is no automated solution and you have to make a choice at some point. You can make the wrong choice so you keep track of the moves taken from the making of the choice.

I'll run your puzzle through my original on Monday night and see what it comes up with


LinkedIn Profile
Newbie on www.simple-talk.com
Post #289985
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse