Advertisement
vovkakorben

Untitled

Nov 21st, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def check_matrix(m):
  2.     pass
  3.    
  4. def get_range_for_cell(m,x,y):
  5.     r = [1,2,3,4,5]
  6.     for j in range(size):
  7.         try:
  8.             i = r.index(m[y][j])
  9.         except ValueError: pass
  10.         else: del r[i]
  11.  
  12.         try:
  13.             i = r.index(m[j][x])
  14.         except ValueError: pass
  15.         else: del r[i]
  16.  
  17.  
  18.     #print(len(r))
  19.     return r
  20.  
  21. def solve(m):
  22.     d = []
  23.     modified = 0
  24.     while (True):
  25.         for y in range(size):
  26.             for x in range(size):
  27.                
  28.                 if (m[y][x] == 0):
  29.                     rng = get_range_for_cell(m,x,y) # searching possible variants for cell
  30.                     if len(rng) == 1:
  31.                         m[y][x] = rng[0] # just one variant - put it
  32.                         modified += 1
  33.                     else:
  34.                         d.append({'x':x, 'y':y, 'range':rng, 'pos':0 }) # put variants and set pointer to zero
  35.         if (modified == 0):
  36.             break  
  37.         modified = 0        
  38.     print(m)    
  39.  
  40. matrix = [
  41.     [5,1,0,2,0],
  42.     [0,0,0,5,1],
  43.     [4,5,1,0,0],
  44.     [0,0,0,4,5],
  45.     [0,4,5,0,3]]
  46. size = len(matrix)
  47. solve(matrix)    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement