Sudoku: Difference between revisions

3,299 bytes added ,  14 years ago
→‎{{header|PicoLisp}}: Added PureBasic
(→‎{{header|PicoLisp}}: Added PureBasic)
Line 1,149:
+---+---+---+---+---+---+---+---+---+
a b c d e f g h i</pre>
=={{header|PureBasic}}==
A brute force method is used, it seemed the fastest as well as the simplest.
<lang PureBasic>DataSection
puzzle:
Data.s "394002670"
Data.s "000300400"
Data.s "500690020"
Data.s "045000900"
Data.s "600000007"
Data.s "007000580"
Data.s "010067008"
Data.s "009008000"
Data.s "026400735"
EndDataSection
 
#IsPossible = 0
#IsNotPossible = 1
#Unknown = 0
Global Dim sudoku(8, 8)
;-declarations
Declare readSudoku()
Declare displaySudoku()
Declare.s buildpossible(x, y, Array possible.b(1))
Declare solvePuzzle(x = 0, y = 0)
 
;-procedures
Procedure readSudoku()
Protected a$, row, column
Restore puzzle
For row = 0 To 8
Read.s a$
For column = 0 To 8
sudoku(column, row) = Val(Mid(a$, column + 1, 1))
Next
Next
EndProcedure
 
Procedure displaySudoku()
Protected row, column
Static border.s = "+-----+-----+-----+"
For row = 0 To 8
If row % 3 = 0: PrintN(border): EndIf
For column = 0 To 8
If column % 3 = 0: Print("|"): Else: Print(" "): EndIf
If sudoku(column, row): Print(Str(sudoku(column, row))): Else: Print("."): EndIf
Next
PrintN("|")
Next
PrintN(border)
EndProcedure
 
Procedure.s buildpossible(x, y, Array possible.b(1))
Protected index, column, row, boxColumn = (x / 3) * 3, boxRow = (y / 3) * 3
Dim possible.b(9)
 
For index = 0 To 8
possible(sudoku(index, y)) = #IsNotPossible ;record possibles in column
possible(sudoku(x, index)) = #IsNotPossible ;record possibles in row
Next
;record possibles in box
For row = boxRow To boxRow + 2
For column = boxColumn To boxColumn + 2
possible(sudoku(column, row)) = #IsNotPossible
Next
Next
EndProcedure
 
Procedure solvePuzzle(x = 0, y = 0)
Protected row, column, spot, digit
Dim possible.b(9)
For row = y To 8
For column = x To 8
If sudoku(column, row) = #Unknown
buildpossible(column, row, possible())
For digit = 1 To 9
If possible(digit) = #IsPossible
sudoku(column, row) = digit
spot = row * 9 + column + 1
If solvePuzzle(spot % 9, spot / 9)
Break 3
EndIf
EndIf
Next
 
If digit = 10
sudoku(column, row) = #Unknown
ProcedureReturn #False
EndIf
EndIf
Next
x = 0 ;reset column start point
Next
ProcedureReturn #True
EndProcedure
 
If OpenConsole()
readSudoku()
displaySudoku()
If solvePuzzle()
PrintN("Solved.")
displaySudoku()
Else
PrintN("Unable to solve puzzle") ;due to bad starting data
EndIf
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf
</lang>
Sample output:
<pre>+-----+-----+-----+
|3 9 4|. . 2|6 7 .|
|. . .|3 . .|4 . .|
|5 . .|6 9 .|. 2 .|
+-----+-----+-----+
|. 4 5|. . .|9 . .|
|6 . .|. . .|. . 7|
|. . 7|. . .|5 8 .|
+-----+-----+-----+
|. 1 .|. 6 7|. . 8|
|. . 9|. . 8|. . .|
|. 2 6|4 . .|7 3 5|
+-----+-----+-----+
Solved.
+-----+-----+-----+
|3 9 4|8 5 2|6 7 1|
|2 6 8|3 7 1|4 5 9|
|5 7 1|6 9 4|8 2 3|
+-----+-----+-----+
|1 4 5|7 8 3|9 6 2|
|6 8 2|9 4 5|3 1 7|
|9 3 7|1 2 6|5 8 4|
+-----+-----+-----+
|4 1 3|5 6 7|2 9 8|
|7 5 9|2 3 8|1 4 6|
|8 2 6|4 1 9|7 3 5|
+-----+-----+-----+</pre>
 
=={{header|Python}}==
Anonymous user