|
| 1 | +# A Backtracking program in Python to solve Sudoku problem |
| 2 | + |
| 3 | + |
| 4 | +# A Utility Function to print the Grid |
| 5 | +def print_grid(arr): |
| 6 | + for i in range(9): |
| 7 | + for j in range(9): |
| 8 | + print arr[i][j], |
| 9 | + print ('n') |
| 10 | + |
| 11 | + |
| 12 | +# Function to Find the entry in the Grid that is still not used |
| 13 | +# Searches the grid to find an entry that is still unassigned. If |
| 14 | +# found, the reference parameters row, col will be set the location |
| 15 | +# that is unassigned, and true is returned. If no unassigned entries |
| 16 | +# remain, false is returned. |
| 17 | +# 'l' is a list variable that has been passed from the solve_sudoku function |
| 18 | +# to keep track of incrementation of Rows and Columns |
| 19 | +def find_empty_location(arr,l): |
| 20 | + for row in range(9): |
| 21 | + for col in range(9): |
| 22 | + if(arr[row][col]==0): |
| 23 | + l[0]=row |
| 24 | + l[1]=col |
| 25 | + return True |
| 26 | + return False |
| 27 | + |
| 28 | +# Returns a boolean which indicates whether any assigned entry |
| 29 | +# in the specified row matches the given number. |
| 30 | +def used_in_row(arr,row,num): |
| 31 | + for i in range(9): |
| 32 | + if(arr[row][i] == num): |
| 33 | + return True |
| 34 | + return False |
| 35 | + |
| 36 | +# Returns a boolean which indicates whether any assigned entry |
| 37 | +# in the specified column matches the given number. |
| 38 | +def used_in_col(arr,col,num): |
| 39 | + for i in range(9): |
| 40 | + if(arr[i][col] == num): |
| 41 | + return True |
| 42 | + return False |
| 43 | + |
| 44 | +# Returns a boolean which indicates whether any assigned entry |
| 45 | +# within the specified 3x3 box matches the given number |
| 46 | +def used_in_box(arr,row,col,num): |
| 47 | + for i in range(3): |
| 48 | + for j in range(3): |
| 49 | + if(arr[i+row][j+col] == num): |
| 50 | + return True |
| 51 | + return False |
| 52 | + |
| 53 | +# Checks whether it will be legal to assign num to the given row,col |
| 54 | +# Returns a boolean which indicates whether it will be legal to assign |
| 55 | +# num to the given row,col location. |
| 56 | +def check_location_is_safe(arr,row,col,num): |
| 57 | + |
| 58 | + # Check if 'num' is not already placed in current row, |
| 59 | + # current column and current 3x3 box |
| 60 | + return not used_in_row(arr,row,num) and not used_in_col(arr,col,num) and not used_in_box(arr,row - row%3,col - col%3,num) |
| 61 | + |
| 62 | +# Takes a partially filled-in grid and attempts to assign values to |
| 63 | +# all unassigned locations in such a way to meet the requirements |
| 64 | +# for Sudoku solution (non-duplication across rows, columns, and boxes) |
| 65 | +def solve_sudoku(arr): |
| 66 | + |
| 67 | + # 'l' is a list variable that keeps the record of row and col in find_empty_location Function |
| 68 | + l=[0,0] |
| 69 | + |
| 70 | + # If there is no unassigned location, we are done |
| 71 | + if(not find_empty_location(arr,l)): |
| 72 | + return True |
| 73 | + |
| 74 | + # Assigning list values to row and col that we got from the above Function |
| 75 | + row=l[0] |
| 76 | + col=l[1] |
| 77 | + |
| 78 | + # consider digits 1 to 9 |
| 79 | + for num in range(1,10): |
| 80 | + |
| 81 | + # if looks promising |
| 82 | + if(check_location_is_safe(arr,row,col,num)): |
| 83 | + |
| 84 | + # make tentative assignment |
| 85 | + arr[row][col]=num |
| 86 | + |
| 87 | + # return, if sucess, ya! |
| 88 | + if(solve_sudoku(arr)): |
| 89 | + return True |
| 90 | + |
| 91 | + # failure, unmake & try again |
| 92 | + arr[row][col] = 0 |
| 93 | + |
| 94 | + # this triggers backtracking |
| 95 | + return False |
| 96 | + |
| 97 | +# Driver main function to test above functions |
| 98 | +if __name__=="__main__": |
| 99 | + |
| 100 | + # creating a 2D array for the grid |
| 101 | + grid=[[0 for x in range(9)]for y in range(9)] |
| 102 | + |
| 103 | + # assigning values to the grid |
| 104 | + grid=[[3,0,6,5,0,8,4,0,0], |
| 105 | + [5,2,0,0,0,0,0,0,0], |
| 106 | + [0,8,7,0,0,0,0,3,1], |
| 107 | + [0,0,3,0,1,0,0,8,0], |
| 108 | + [9,0,0,8,6,3,0,0,5], |
| 109 | + [0,5,0,0,9,0,6,0,0], |
| 110 | + [1,3,0,0,0,0,2,5,0], |
| 111 | + [0,0,0,0,0,0,0,7,4], |
| 112 | + [0,0,5,2,0,6,3,0,0]] |
| 113 | + |
| 114 | + # if sucess print the grid |
| 115 | + if(solve_sudoku(grid)): |
| 116 | + print_grid(grid) |
| 117 | + else: |
| 118 | + print "No solution exists" |
0 commit comments