-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolveSudoku.py
69 lines (67 loc) · 2.25 KB
/
solveSudoku.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution:
#check the value in i, j is valid
def isValidSudoku(self, board,i,j):
v = board[i][j]
if v == '.':
return True
if int(v)>9:
return False
for n in xrange(9):
c_hor = board[i][n]
if c_hor == v and n != j:
return False
c_ver = board[n][j]
if c_ver == v and n != i:
return False
for n in xrange(3):
for m in xrange(3):
i_hor = (i/3)*3+n
j_ver = (j/3)*3+m
c_temple = board[i_hor][j_ver]
if c_temple == v and (i !=i_hor or j !=j_ver):
return False
return True
# @param {character[][]} board
# @return {void} Do not return anything, modify board in-place instead.
def solveSudoku(self, board):
indexes = []
for i in xrange(9):
for j in xrange(9):
if board[i][j] == '.':
index= i*9+j
indexes.append(index)
l = len(indexes)
p = 0
while (p >= 0 and p < l):
index = indexes[p]
i = index/9
j = index%9
if board[i][j] == '.':
# fill new
for q in range(1,10):
board[i][j] = str(q)
if self.isValidSudoku(board, i,j) == True:
break
else :
board[i][j] = '.'
if board[i][j] == '.':
p-=1 # back trace
else:
p+=1 # next
else:
if board[i][j]=='9':
board[i][j] = '.'
p -= 1
continue
#else
board[i][j] = str(int(board[i][j]) + 1)
while(self.isValidSudoku(board,i,j)==False):
if board[i][j] == '9':
break
else:
board[i][j] = str(int(board[i][j])+1)
if self.isValidSudoku(board, i, j):
p +=1 #next
else:
board[i][j] = '.'
p -=1 # back trace