-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.py
42 lines (36 loc) · 1006 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def solve(cells):
find = find_empty(cells)
if not find:
return True
else:
row,col = find
for i in range(1,10):
if valid(cells,i,(row,col)):
cells[row][col]=i
if solve(cells):
return True
cells[row][col]=0
return False
def valid(cells, num,pos):
for i in range(len(cells[0])):
if cells[pos[0]][i]==num and pos[1]!=i:
return False
for i in range(len(cells)):
if cells[i][pos[1]]==num and pos[0]!=i:
return False
box_x=pos[1]//3
box_y = pos[0]//3
for i in range(box_y*3,box_y*3+3):
for j in range(box_x*3,box_x*3+3):
if cells[i][j]==num and (i,j)!=pos:
return False
return True
def find_empty(cells):
for i in range(len(cells)):
for j in range(len(cells[0])):
if cells[i][j]==0:
return(i,j)
return None
def sudoku_solver(board):
solve(board)
return board