Advertisement
Shaiai

Sudoku Solver Project

Oct 31st, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. #Program that can solve Sudoku Boards using backtracking / recursion
  2.  
  3. #The layout of the board
  4. board = [[5,3,0,0,7,0,0,0,0],
  5.          [6,0,0,1,9,5,0,0,0],
  6.          [0,9,8,0,0,0,0,6,0],
  7.          [8,0,0,0,6,0,0,0,3],
  8.          [4,0,0,8,0,3,0,0,1],
  9.          [7,0,0,0,2,0,0,0,6],
  10.          [0,6,0,0,0,0,2,8,0],
  11.          [0,0,0,4,1,9,0,0,5],
  12.          [0,0,0,0,8,0,0,7,9]]
  13.  
  14.  
  15. def solve(board):
  16.   #Find the Empty Cell
  17.   empty_cell = find_zero(board)
  18.  
  19.   #If there is no empty cell you return the board, it is complete
  20.   if empty_cell is None:
  21.     return print_board(board)
  22.    
  23.   #Keep Track of the row and column of your empty cell
  24.   empty_row = empty_cell[0]
  25.   empty_col = empty_cell[1]
  26.  
  27.   #Try filling those with numbers from 1 to 9
  28.   for i in range(1,10):
  29.     if is_valid(board, empty_row, empty_col, i):
  30.       board[empty_row][empty_col] = i
  31.       solve(board)
  32.       board[empty_row][empty_col] = 0
  33.   return None
  34.  
  35. #Function that will display the board
  36. def print_board(board):
  37.   if board is None:
  38.     return None
  39.   for row in board:
  40.     row_print = ""
  41.     for value in row:
  42.       row_print += str(value) + " "
  43.     print(row_print)
  44.  
  45. #Function to find the next Zero in the puzzle
  46. def find_zero(board):
  47.   if board is None:
  48.     return "There is no board"
  49.  
  50.   empty_cell = []
  51.   for i in range(len(board)):
  52.     for j in range(len(board[0])):
  53.  
  54.       if board[i][j] == 0:
  55.        empty_cell.append(i)
  56.        empty_cell.append(j)
  57.        return empty_cell
  58.        
  59.       else:
  60.         None
  61.  
  62. #Function to check if the current value entered is valid by seeing if it's already
  63. #In the corresponding row or column
  64. def is_valid(board, row, col, value):
  65.  
  66.     for i in range(len(board)):
  67.       if board[row][i] == value:
  68.         return False
  69.    
  70.     for i in range(len(board)):
  71.       if board[col][i] == value:
  72.         return False
  73.    
  74.     x = (row//3)*3
  75.     y = (col//3)*3
  76.     for i in range(3):
  77.       for j in range(3):
  78.         if board[x+i][y+j] == value:
  79.           return False
  80.     return True                                                
  81.  
  82.  
  83.  
  84. #Test case print outs
  85. print(find_zero(board))
  86. print(is_valid(board, 0 , 2 , 1 ))
  87. print(is_valid(board, 0 , 2 , 7 ))
  88. print(is_valid(board, 0 , 2 , 6 ))
  89. print(is_valid(board, 3 , 5 , 2 ))
  90. solve(board)
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement