Advertisement
Vimes

Sudoku

Feb 19th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from pprint import pprint
  3. board = [
  4.         [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. pojebane = [
  16.         [8,0,0,0,0,0,0,0,0],
  17.         [0,0,3,6,0,0,0,0,0],
  18.         [0,7,0,0,9,0,2,0,0],
  19.         [0,5,0,0,0,7,0,0,0],
  20.         [0,0,0,0,4,5,7,0,0],
  21.         [0,0,0,1,0,0,0,3,0],
  22.         [0,0,1,0,0,0,0,6,8],
  23.         [0,0,8,5,0,0,0,1,0],
  24.         [0,9,0,0,0,0,4,0,0]
  25. ]
  26. pprint (board)
  27. def is_possible (x,y,n,board) :
  28.     for i in range(9):
  29.         if board[x][i] == n :
  30.             return False
  31.         if board[i][y] == n :
  32.             return False
  33.     xmin = (x // 3) * 3
  34.     ymin = (y // 3) * 3
  35.     for i in range(xmin, xmin+3):
  36.         for j in range(ymin, ymin+3):
  37.             if board[i][j] ==  n :
  38.                 return False
  39.     return True
  40.  
  41. def solve (board):
  42.     for x in range(9):
  43.         for y in range(9):
  44.             if  board[x][y] == 0 :
  45.                 for n in range(1, 10):
  46.                     if is_possible(x,y,n,board):
  47.                         board[x][y] = n
  48.                         solve(board)
  49.                         board[x][y] = 0
  50.                 return
  51.     pprint (board)
  52.  
  53. solve(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement