woodsja

Untitled

Dec 16th, 2018
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. import numpy as np
  2.  
  3. limit = 10000
  4. backtracks = 0
  5. generate_pair_flag = False
  6.  
  7.  
  8. def makeRandomSudoku(number_of_randoms):
  9.     # generates a random board with no constraints
  10.     # board has number_of_randoms filled in values with all other values as zero
  11.     grid_list = [0] * 81
  12.     locations = np.random.choice(81, number_of_randoms, replace=False)
  13.     for location in locations:
  14.         grid_list[location] = int(np.random.randint(1, high=10))
  15.  
  16.     i = 0
  17.     grid = []
  18.     while i < len(grid_list):
  19.         grid.append(grid_list[i:i + 9])
  20.         i += 9
  21.     return grid
  22.  
  23.  
  24. def findNextCellToFill(grid):
  25.     # Takes a 9 by 9 grid and looks row by column for the first zero.
  26.     # Returns either x, y coordinates of first '0' or -1, -1
  27.     for x in range(0, 9):
  28.         for y in range(0, 9):
  29.             if grid[x][y] == 0:
  30.                 return x, y
  31.     return -1, -1
  32.  
  33.  
  34. def isValid(grid, i, j, e):
  35.     # takes a 9 by 9 grid; i,j coordinates; and a number 'e' then looks row-wise, column-wise, and sector-wise for duplicates of the number 'e''
  36.     # returns True if passes row/column/sector checks.
  37.     # returns False otherwise
  38.     rowOk = all([e != grid[i][x] for x in range(9)])
  39.     if rowOk:
  40.         columnOk = all([e != grid[x][j] for x in range(9)])
  41.         if columnOk:
  42.             # find the top left x, y coordinate of section containing the i,j cell
  43.             secTopX, secTopY = 3*(i//3), 3*(j//3)
  44.             for x in range(secTopX, secTopX+3):
  45.                 for y in range(secTopY, secTopY+3):
  46.                     if grid[x][y] == e:
  47.                         return False
  48.             return True
  49.     return False
  50.  
  51.  
  52. # In[7]:
  53.  
  54.  
  55.  
  56. def solveSudoku(grid, i=0, j=0):
  57.     # fills in missing squares of Sudoku puzzle using rules with a brute-force guess/check
  58.     # takes a grid and starts looking in the top left corner i,j = 0,0
  59.     global limit
  60.     global backtracks
  61.     if backtracks < limit:
  62.         i, j = findNextCellToFill(grid)
  63.         if i == -1:
  64.             # we solved it or there's nothing left to solve
  65.             return True
  66.  
  67.         for e in range (1, 10):
  68.             # try different values in i, j location
  69.             if isValid(grid, i, j, e):
  70.                 grid[i][j] = e
  71.                 if solveSudoku(grid, i, j):
  72.                     return True
  73.  
  74.                 # undo current cell for backtracking
  75.                 if backtracks < limit:
  76.                     backtracks += 1
  77.                     grid[i][j] = 0
  78.         else:
  79.             # loop fell through without filling a number
  80.             return False
  81.  
  82.  
  83.  
  84.     else:
  85.         return False
  86.        
  87.  
  88.  
  89. def printSudoku(grid):
  90.     numrow = 0
  91.     for row in grid:
  92.         if numrow % 3 == 0 and numrow != 0:
  93.             print(' ')
  94.         print(row[0:3], ' ', row[3:6], ' ', row[6:9])
  95.         numrow += 1
  96.     return
  97.  
  98. def generate_pair():
  99.     global backtracks
  100.     backtracks = 0
  101.     # generates an unsolved and solved grid
  102.     solved = makeRandomSudoku(10)
  103.     unsolved = solved.copy()
  104.     if solveSudoku(solved):
  105.         return unsolved, solved
  106.     else:
  107.         backtracks = 0
  108.         return generate_pair()
  109.  
  110.  
  111. # asdf = makeRandomSudoku(5)
  112. # printSudoku(asdf)
  113. # asdf1 = asdf[:][:]
  114. # print("\n")
  115. # solveSudoku(asdf1)
  116. # printSudoku(asdf1)
  117. # print("\n")
  118. # printSudoku(asdf)
  119.  
  120.  
  121. asdf1, asdf2 = generate_pair()
  122. print(asdf1)
  123. print(asdf2)
Advertisement
Add Comment
Please, Sign In to add comment