1- class Solution (object ):
2- def solveSudoku (self , board ):
1+ from collections import defaultdict
2+ import copy
3+ class Solution :
4+ def solveSudoku (self , board : List [List [str ]]) -> None :
35 """
4- :type board: List[List[str]]
5- :rtype: None Do not return anything, modify board in-place instead.
6+ Do not return anything, modify board in-place instead.
67 """
7- from collections import defaultdict
8- row , column , squre = defaultdict (set ), defaultdict (set ), defaultdict (set )
9-
8+ row = defaultdict (set )
9+ col = defaultdict (set )
10+ square = defaultdict (set )
11+ for i in range (9 ):
12+ for j in range (9 ):
13+ if board [i ][j ].isdigit ():
14+ row [i ].add (board [i ][j ])
15+ col [j ].add (board [i ][j ])
16+ square [(i // 3 , j // 3 )].add (board [i ][j ])
1017 self .res = []
1118 def dfs (x , y ):
12-
13- if x == 8 and y == 9 :
14- # print board
15- for roww in board :
16- self .res .append (roww [:])
17- # print self.res
19+ if x == 9 and y == 0 :
20+ if self .isValidSudoku (board ):
21+ self .res = copy .deepcopy (board )
1822 return
19- if y == 9 :
20- dfs (x + 1 , 0 )
21- return
22- if board [x ][y ].isdigit ():
23- dfs (x , y + 1 )
24- return
25-
26- for k in range (1 ,10 ):
27- if str (k ) not in row [x ] and str (k ) not in column [y ] and str (k ) not in squre [(x // 3 , y // 3 )]:
28- board [x ][y ] = str (k )
29- row [x ].add (str (k ))
30- column [y ].add (str (k ))
31- squre [(x // 3 , y // 3 )].add (str (k ))
32-
33- dfs (x , y + 1 )
34-
35- board [x ][y ] = "."
36- row [x ].remove (str (k ))
37- column [y ].remove (str (k ))
38- squre [(x // 3 , y // 3 )].remove (str (k ))
3923
24+ if not self .res :
25+ if board [x ][y ] != "." :
26+ if y == 8 :
27+ dfs (x + 1 , 0 )
28+ else :
29+ dfs (x , y + 1 )
30+ return
31+
32+ for num in range (1 , 10 ):
33+ num = str (num )
34+ if num not in row [x ] and num not in col [y ] and num not in square [(x // 3 , y // 3 )]:
35+ board [x ][y ] = num
36+
37+ row [x ].add (num )
38+ col [y ].add (num )
39+ square [(x // 3 , y // 3 )].add (num )
40+ if y == 8 :
41+ dfs (x + 1 , 0 )
42+ else :
43+ dfs (x , y + 1 )
44+ board [x ][y ] = "."
45+ row [x ].remove (num )
46+ col [y ].remove (num )
47+ square [(x // 3 , y // 3 )].remove (num )
48+
49+ dfs (0 , 0 )
50+ board [:] = self .res
51+
52+
53+ def isValidSudoku (self , board : List [List [str ]]) -> bool :
54+ row = defaultdict (set )
55+ col = defaultdict (set )
56+ square = defaultdict (set )
4057 for i in range (9 ):
4158 for j in range (9 ):
4259 if board [i ][j ].isdigit ():
43- row [i ].add (board [i ][j ].encode ("utf-8" ))
44- column [j ].add (board [i ][j ].encode ("utf-8" ))
45- squre [(i // 3 , j // 3 )].add (board [i ][j ].encode ("utf-8" ))
46-
47- dfs (0 , 0 )
48- board [:] = self .res
60+ if board [i ][j ] in row [i ] or board [i ][j ] in col [j ] or board [i ][j ] in square [(i // 3 , j // 3 )]:
61+ return False
62+ else :
63+ row [i ].add (board [i ][j ])
64+ col [j ].add (board [i ][j ])
65+ square [(i // 3 , j // 3 )].add (board [i ][j ])
66+ return True
0 commit comments