Advertisement
csmine

Concours blanc d'informatique : fonction "solutionSudoku"

Jun 11th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3. def rechercheDansListe(elt,L):
  4.     for i in range(0,len(L)):
  5.         if L[i]==elt:
  6.             return(i)
  7.     return(-1)
  8.  
  9. def caseSuivante(pos):
  10.     x,y = pos[0],pos[1]
  11.     if y==8:
  12.         y=0
  13.         x+=1
  14.     else:
  15.         y+=1
  16.     return([x,y])
  17.  
  18. def casePrecedente(pos):
  19.     x,y = pos[0],pos[1]
  20.     if y==0:
  21.         y=8
  22.         x=x-1
  23.     else:
  24.         y=y-1
  25.     return([x,y])
  26.    
  27. def caseLibreSuivante(pos,L):
  28.     suivante=caseSuivante(pos)
  29.     if suivante[0]==9 or L[suivante[0]][suivante[1]]==0:
  30.         return(suivante)
  31.     else:
  32.         return(caseLibreSuivante(suivante,L))
  33.        
  34. def caseLibrePrecedente(pos,L):
  35.     prec=casePrecedente(pos)
  36.     if prec[0]==-1 or L[prec[0]][prec[1]]==0:
  37.         return(prec)
  38.     else:
  39.         return(caseLibrePrecedente(prec,L))
  40.        
  41.        
  42. def ligne(L,i,j):
  43.     chiffre=[]
  44.     for k in range(0,9):
  45.         if L[i][k]>0 and k!=j:
  46.             chiffre.append(L[i][k])
  47.     return chiffre
  48.  
  49. def colonne(L,i,j):
  50.     chiffre=[]
  51.     for k in range(0,9):
  52.         if L[k][i]>0 and k!=j:
  53.             chiffre.append(L[k][i])
  54.     return chiffre
  55.  
  56. def carre(L,i,j):
  57.     icoin = 3*(i//3)
  58.     jcoin=3*(j//3)
  59.     chiffre=[]
  60.     for x in range(icoin, icoin+3):
  61.         for y in range(jcoin,jcoin+3):
  62.             if (L[x][y] > 0 and (x,y)!=(i,j)):
  63.                 chiffre.append(L[x][y])
  64.     return chiffre
  65.  
  66.  
  67. def conflit(L,i,j):
  68.     return(ligne(L,i,j) + colonne(L,i,j) + carre(L,i,j))
  69.  
  70. def chiffresOk(L,i,j):
  71.     listeOk=[]
  72.     listeConf=conflit(L,i,j)
  73.     for k in range(1,10):
  74.         if k not in listeConf:
  75.             listeOk.append(k)
  76.     return(listeOk)
  77.        
  78. def solutionSudoku(L):
  79.         M=deepcopy(L)
  80.         pos = caseLibreSuivante([-1,8],L)
  81.        
  82.         compteur=0
  83.        
  84.         while pos[0]>=0 and pos[0]<=8 and compteur<5:
  85.             compteur+=1
  86.            
  87.             Elt = M[pos[0]][pos[1]]
  88.             M[pos[0]][pos[1]] = 0
  89.             listeChiffresOk=chiffresOk(M,pos[0],pos[1])
  90.             if len(listeChiffresOk)==0:
  91.                 pos=caseLibrePrecedente(pos,L)
  92.             elif Elt==0:
  93.                 M[pos[0]][pos[1]]=listeChiffresOk[0]
  94.                 pos = caseLibreSuivante(pos,L)
  95.             else:
  96.                 i = rechercheDansListe(Elt,listeChiffresOk)
  97.                 if i==len(listeChiffresOk)-1:
  98.                     pos=caseLibrePrecedente(pos,L)
  99.                 else:
  100.                     M[pos[0]][pos[1]]=listeChiffresOk[i+1]
  101.                     pos=caseLibreSuivante(pos,L)
  102.             print(compteur)
  103.             print("pos",pos)
  104.             print("elt",Elt)
  105.             print("listeChiffresOk",listeChiffresOk)
  106.             print("M0",M[0])
  107.             print("------")
  108.         return(M)
  109.  
  110. L = [   [2,5,4,0,9,6,3,0,0],
  111.         [0,1,9,0,8,0,0,7,4],
  112.         [0,0,8,4,0,0,6,2,0],
  113.         [5,9,0,6,2,1,0,0,0],
  114.         [0,2,7,0,0,0,1,6,0],
  115.         [0,0,0,5,7,4,0,9,3],
  116.         [0,8,5,0,0,9,7,0,0],
  117.         [9,3,0,0,5,0,8,4,0],
  118.         [0,0,2,0,6,0,0,0,1]]
  119.  
  120. ####################################
  121. """ La suite est ce qu'il se passe en exécutant "solutionSudoku(L)" (avec les print) :
  122. 1
  123. pos [0, 7]
  124. elt 0
  125. listeChiffresOk [1, 7]
  126. M0 [2, 5, 4, 1, 9, 6, 3, 0, 0]
  127. ------
  128. 2
  129. pos [0, 8]
  130. elt 0
  131. listeChiffresOk [8]
  132. M0 [2, 5, 4, 1, 9, 6, 3, 8, 0]
  133. ------
  134. 3
  135. pos [0, 7]
  136. elt 0
  137. listeChiffresOk []
  138. M0 [2, 5, 4, 1, 9, 6, 3, 8, 0] # Le corrigé donne [2,5,4,1,9,6,3,0,0]
  139. ------
  140. 4
  141. pos [0, 3]
  142. elt 8
  143. listeChiffresOk [8]
  144. M0 [2, 5, 4, 1, 9, 6, 3, 0, 0] # Le corrigé donne [2,5,4,0,9,6,3,0,0]
  145. ------
  146. 5
  147. pos [0, 7]
  148. elt 1
  149. listeChiffresOk [1, 7]
  150. M0 [2, 5, 4, 7, 9, 6, 3, 0, 0]
  151. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement