Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
87
Task/Sudoku/FreeBASIC/sudoku.basic
Normal file
87
Task/Sudoku/FreeBASIC/sudoku.basic
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
Dim Shared As Integer cuadricula(9, 9), cuadriculaResuelta(9, 9)
|
||||
|
||||
Function isSafe(i As Integer, j As Integer, n As Integer) As Boolean
|
||||
Dim As Integer iMin, jMin, f, c
|
||||
|
||||
If cuadricula(i, j) <> 0 Then Return (cuadricula(i, j) = n)
|
||||
|
||||
'cuadricula(i, j) es una celda vacía. Compruebe si n está OK
|
||||
'primero revisa la fila i
|
||||
For f = 1 To 9
|
||||
If cuadricula(i, f) = n Then Return False
|
||||
Next f
|
||||
|
||||
'ahora comprueba la columna j
|
||||
For c = 1 To 9
|
||||
If cuadricula(c, j) = n Then Return False
|
||||
Next c
|
||||
|
||||
'finalmente, compruebe el subcuadrado de 3x3 que contiene cuadricula(i,j)
|
||||
iMin = 1 + 3 * Int((i - 1) / 3)
|
||||
jMin = 1 + 3 * Int((j - 1) / 3)
|
||||
For c = iMin To iMin + 2
|
||||
For f = jMin To jMin + 2
|
||||
If cuadricula(c, f) = n Then Return False
|
||||
Next f
|
||||
Next c
|
||||
|
||||
'todas las pruebas estuvieron OK
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Sub Resolver(i As Integer, j As Integer)
|
||||
Dim As Integer f, c, n, temp
|
||||
If i > 9 Then
|
||||
'salir con cuadriculaResuelta = cuadricula
|
||||
For c = 1 To 9
|
||||
For f = 1 To 9
|
||||
cuadriculaResuelta(c, f) = cuadricula(c, f)
|
||||
Next f
|
||||
Next c
|
||||
Exit Sub
|
||||
End If
|
||||
For n = 1 To 9
|
||||
If isSafe(i, j, n) Then
|
||||
temp = cuadricula(i, j)
|
||||
cuadricula(i, j) = n
|
||||
If j = 9 Then
|
||||
Resolver i + 1, 1
|
||||
Else
|
||||
Resolver i, j + 1
|
||||
End If
|
||||
cuadricula(i, j) = temp
|
||||
End If
|
||||
Next n
|
||||
End Sub
|
||||
|
||||
Dim As String s(9)
|
||||
'inicializar la cuadrícula usando 9 cadenas, una por fila
|
||||
s(1) = "001005070"
|
||||
s(2) = "920600000"
|
||||
s(3) = "008000600"
|
||||
s(4) = "090020401"
|
||||
s(5) = "000000000"
|
||||
s(6) = "304080090"
|
||||
s(7) = "007000300"
|
||||
s(8) = "000007069"
|
||||
s(9) = "010800700"
|
||||
|
||||
Dim As Integer i, j
|
||||
For i = 1 To 9
|
||||
For j = 1 To 9
|
||||
cuadricula(i, j) = Int(Val(Mid(s(i), j, 1)))
|
||||
Next j
|
||||
Next i
|
||||
|
||||
Resolver 1, 1
|
||||
Print "Solucion:"
|
||||
Color 12: Print "---------+---------+---------"
|
||||
For i = 1 To 9
|
||||
For j = 1 To 9
|
||||
Color 7: Print cuadriculaResuelta(i, j); " ";
|
||||
Color 12
|
||||
If (j Mod 3 = 0) And (j <> 9) Then Color 12: Print "|";
|
||||
Next j
|
||||
If (i Mod 3 = 0) Then Print !"\n---------+---------+---------" Else Print
|
||||
Next i
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue