SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Sudoko


SQL Sudoko

Author
Message
Andrew Clarke-161005
Andrew Clarke-161005
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: Administrators
Points: 147 Visits: 52
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
Tom Pettijohn
Tom Pettijohn
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 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|
*-----------*


Me Da Man
Me Da Man
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 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


Freddie Davis
Freddie Davis
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 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


Dave Poole
Dave Poole
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16226 Visits: 3403
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
www.simple-talk.com
Rob Scholl
Rob Scholl
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 354
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 '*','-','-','-','-','-','-','-','-','-','-','-','*'
Kenneth Gladden
Kenneth Gladden
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 37

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

Thank you very much!


Dave Poole
Dave Poole
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16226 Visits: 3403
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
www.simple-talk.com
Daryl AZ
Daryl AZ
Mr or Mrs. 500
Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)

Group: General Forum Members
Points: 528 Visits: 698
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
Dave Poole
Dave Poole
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16226 Visits: 3403
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
www.simple-talk.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search