Advertisement
AyanUpadhaya

Sudoku Solver in Python Script

Aug 1st, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. def find_next_empty(puzzle):
  2.  
  3.     # finds the next,row,col on the puzzle that's not filled yet --> rep with -1
  4.     # return row,col tuple(or (none,none) ) if there is None
  5.     # keep in mind that we are using 0-8 for our indices
  6.  
  7.     for r in range(9):
  8.         for c in range(9):
  9.             if puzzle[r][c] == -1:
  10.                 return r,c
  11.  
  12.     return None, None # if no space left
  13.  
  14.  
  15. def is_valid(puzzle,guess,row,col):
  16.     # figures out whether the guess at the row/col is valid guess
  17.     # returns True if valid, returns False otherwise
  18.  
  19.     row_vals = puzzle[row]
  20.  
  21.     if guess in row_vals:
  22.         return False
  23.  
  24.     #columns
  25.  
  26.     col_vals=[puzzle[i][col] for i in range(9)]
  27.  
  28.     if guess in col_vals:
  29.         return False
  30.  
  31.     # now the squares
  32.     # I want to get 3x3 square starts
  33.     # and iterate over the 3 values in the row/column
  34.  
  35.     row_start = (row//3)*3 #1//3 = 0 throw away the remainder
  36.     col_start = (col//3)*3
  37.    
  38.     for r in range(row_start,row_start+3):
  39.         for c in range(col_start,col_start+3):
  40.             if puzzle[r][c] == guess:
  41.                 return False
  42.  
  43.     #return this if it reach here
  44.  
  45.     return True
  46.  
  47.  
  48.  
  49. def solve_sudoku(puzzle):
  50.     # solve sudoku using backtracking
  51.     # our puzzle is a list of list
  52.     # where each inner list is a row in our sudoku puzzle
  53.     # return whether a solution exists
  54.     # mutates puzzle to be solution (if solution exists)
  55.  
  56.     # step -1 choose somewhere on the puzzle to make a guess
  57.  
  58.     row, col = find_next_empty(puzzle)
  59.  
  60.     # step 1.1 if there's nowhere left, then we're done because we only allowed valid inputs
  61.  
  62.     if row is None:
  63.         return True
  64.  
  65.     #step 2: if there is a place to put a number, then make a guess between 1 and 9
  66.  
  67.     for guess in range(1,10):
  68.         #step 3: check if this is a valid guess
  69.         #step 3.1: if this is valid then place that guess on the square
  70.  
  71.         if is_valid(puzzle,guess,row,col):
  72.             puzzle[row][col]=guess
  73.  
  74.             #now recurse using this
  75.  
  76.             #step4 recursively call our function
  77.  
  78.             if solve_sudoku(puzzle):
  79.                 return True
  80.  
  81.         # step 5 if not valid or if our guess does not solve the puzzle
  82.  
  83.         puzzle[row][col]=-1 #reset the value
  84.  
  85.     # step 6 if none of the numbers we try didn't work then puzzle is unsolvable
  86.  
  87.     return False
  88.  
  89.  
  90. if __name__=="__main__":
  91.     example_board=[
  92.         [3,9,-1, -1,5,-1, -1,-1,-1],
  93.         [-1,-1,-1, 2,-1,-1, -1,-1,5],
  94.         [-1,-1,-1, 7,1,9, -1,8,-1],
  95.  
  96.         [-1,5,-1, -1,6,8, -1,-1,-1],
  97.         [2,-1,6, -1,-1,3, -1,-1,-1],
  98.         [-1,-1,-1, -1,-1,-1, -1,-1,4],
  99.  
  100.         [5,-1,-1, -1,-1,-1, -1,-1,-1],
  101.         [6,7,-1, 1,-1, 5, -1,4,-1],
  102.         [1,-1,9, -1,-1,-1,  2,-1,-1]
  103.     ]
  104.  
  105.     print(solve_sudoku(example_board))
  106.     print(example_board)
  107.  
  108.  
  109.  
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement