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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #!/usr/bin/env python
#-*- coding:utf-8 -*-
#resolveur de sudoku
#~ fonctions
def init_tab(tableau):
'initialise le tableau à 0'
i,j=0,0
for i in range(9):
tableau+=[[]] # ajoute quatre fois une sous-liste vide:[[],[],[],[]]
for j in range(9):
tableau[i]+=[0] # ajoute trois éléments '0' à chacune des quatre sous-listes vides
def affiche_tab(tableau):
'affiche le sudoku en console'
i,j=0,0
for i in range(9) :
if i%3==0:
print "-------------------------"
for j in range(9):
if j%3==0:
print "|" ,
if tableau[i][j]==0:
print "_",
else:
print tableau[i][j],
print "|"
print "-------------------------"
def caseVide(tableau):
'retourne le nb de case vide'
i,j=0,0
nb=0
for i in range(9):
for j in range(9):
if tableau[i][j]==0:
nb=nb+1
return nb
def testLigne(tableau,nb,ligne):
"présence d'un nombre sur ligne renvoie True"
i=0
ligne = ligne
for i in range(9):
if tableau[ligne][i]==nb:
return True
return False
def testColonne(tableau,nb,colonne):
"présence d'un nombre sur colonne renvoie True"
i=0
colonne = colonne
for i in range(9):
if tableau[i][colonne]==nb:
return True
return False
def testLC(tableau,nb,ligne,colonne):
"présence d'un nombre sur ligne ou colonne renvoie True"
if ((testColonne(tableau,nb,colonne)) or (testLigne(tableau,nb,ligne))):
return True
else :
return False
def coordCarre(valeur):
'renvoie coordonnée du carré'
if valeur <= 2:
return 0
elif valeur >=6:
return 6
else :
return 3
def testCarre(tableau,nb,ligne,colonne):
"renvoie True si nombre present dans carré"
i,j=0,0
i = coordCarre(ligne)
j = coordCarre(colonne)
listeI = [i,i+1,i+2]
listeJ = [j,j+1,j+2]
for i in listeI:
for j in listeJ:
if tableau[i][j]==nb:
return True
return False
def test(tableau,nb,ligne,colonne):
"renvoie True si nb impossible dans case"
if ((testLC(tableau,nb,ligne,colonne)) or (testCarre(tableau,nb,ligne,colonne))):
return True
else :
return False
def resolutionLC(tableau):
"resolution methode ligne colonne"
solutions_trouves=0
i,j=0,0
val=0
for i in range(9):
for j in range(9):
if tableau[i][j]==0:
nb_poss = 0
val_poss=9
x_poss=0
y_poss=0
for val in range(9):
if not(test(tableau,val+1,i,j)):
nb_poss=nb_poss +1
val_poss=val+1
x_poss=i
y_poss=j
if nb_poss==1:
tableau[x_poss][y_poss]=val_poss
solutions_trouves=solutions_trouves+1
if solutions_trouves!=0:
resolutionLC(tableau)
def resolutionC(tableau):
"resolution methode carre"
solutions_trouves=0
i,j=0,0
val =0
for ligne in range(9):
for colonne in range(9):
if tableau[ligne][colonne]==0:
nb_poss = 0
val_poss=9
x_poss=0
y_poss=0
i = coordCarre(ligne)
j = coordCarre(colonne)
listeI = [i,i+1,i+2]
listeJ = [j,j+1,j+2]
for val in range(9):
for i in listeI:
for j in listeJ:
if not(test(tableau,val+1,i,j)):
nb_poss=nb_poss +1
val_poss=val+1
x_poss=i
y_poss=j
if nb_poss==1:
tableau[x_poss][y_poss]=val_poss
solutions_trouves=solutions_trouves+1
if solutions_trouves!=0:
resolutionC(tableau)
def resolution(tableau):
'solveur de sudoku 9x9'
variation=1
while variation !=0:
casevideinit=caseVide(tableau)
resolutionLC(tableau)
resolutionC(tableau)
variation = casevideinit - caseVide(tableau)
#~ programme
#~ tab_inconnu=[]
#~ init_tab(tab_inconnu)
#~
#~
#~ tab_inconnu = [[3,0,0,0,7,6,8,0,0],
#~ [0,6,5,0,4,3,0,7,0],
#~ [9,0,0,8,0,0,0,0,3],
#~ [0,0,2,6,0,0,0,8,0],
#~ [0,3,9,0,8,0,2,6,0],
#~ [0,8,0,0,0,7,5,0,0],
#~ [5,0,0,0,0,2,0,0,7],
#~ [0,4,0,7,1,0,9,5,0],
#~ [0,0,6,5,9,0,0,0,8]]
#~ print "Sudoku à résoudre"
#~ affiche_tab(tab_inconnu)
#~ resolution(tab_inconnu)
#~ print "Solution"
#~ affiche_tab(tab_inconnu) |
Partager