1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| Private Function creer_grille(ByRef grille As Integer(,), ByRef position As Integer) As Boolean
Dim i, j, v As Integer
Dim valeurs = New List(Of Integer)
Dim resultat As Boolean
'retourne la grille trouvée si nous sommes a la fin
If (position = 81) Then
Return True
'GoTo end_creer_grille
End If
'calcul de la ligne et colonne
i = Math.Floor(position / 9)
j = position Mod 9
'liste des nombres de 1 à 9
valeurs.AddRange({1, 2, 3, 4, 5, 6, 7, 8, 9})
'efface les valeurs déjà utilisées
If (v > 0) Then
For k As Integer = 0 To 8
'retire la valeur de la ligne
If (v = Int(grille(i, k))) Then
valeurs.RemoveAt(v - 1)
End If
'retire la valeur de la colonne
If (v = Int(grille(k, j))) Then
valeurs.RemoveAt(v - 1)
End If
'retire la valeur de la zone
If (v = Int(grille(i + Math.Floor(k / 3), j + (k Mod 3)))) Then
valeurs.RemoveAt(v - 1)
End If
Next
End If
'mélange les valeurs
valeurs = shuffleList(valeurs)
For Each v In valeurs
grille(i, j) = v
resultat = creer_grille(grille, position + 1)
If resultat Then
Return resultat
End If
'If (grille.Length > 0) Then
'GoTo end_creer_grille
'creer_grille(grille, position + 1, isOk)
' End If
grille(i, j) = Nothing
Next
Return False
end_creer_grille:
End Function |
Partager