SQL Sudoko

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

    Thank you very much!

  • 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
    
  • 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

  • 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

  • David, my bad. I read the first page of discussions and missed the forum was multi-page.

    Sounds like I need to add more functionality.

    thanks

  • No problems Daryl, there is actually a typo in your data because it has

    2,1,8 AND 3,2,8 which is against the rules as 8 would be in the same 1st 3x3 cell

  • It looks like Daryl is actually putting his data in the table in a different order than you did, David.  His value is coming first with the row and column following.

  • Great Job! Nicely explained too, although it looks like someone's got too much time on his hands

    Michael Gilchrist
    Database Specialist
    There are 10 types of people in the world, those who understand binary and those that don't. 😀

  • So it's ressurecting an old thread but does anyone have the RemoveSolvedCells SP ?

    Thanks,

    Chris

     

  • Chris, it is obsolete.

    When I built the original solution I used temporary tables however I found that views worked just as well and the temporary tables weren't needed.

    The error is in the article.

Viewing 10 posts - 16 through 24 (of 24 total)

You must be logged in to reply to this topic. Login to reply