-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37.解数独.py
66 lines (57 loc) · 1.76 KB
/
37.解数独.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
#
# @lc app=leetcode.cn id=37 lang=python3
#
# [37] 解数独
#
# %%
# @lc code=start
class Solution:
def check(self, board, row, col, num):
board_T = [[board[i][j] for i in range(9)] for j in range(9)]
if num in board[row]:
return False
if num in board_T[col]:
return False
x = row // 3 * 3
y = col // 3 * 3
for i in range(x, x+3):
for j in range(y, y+3):
if num == board[i][j]:
return False
return True
def move(self, board, idx=0):
num = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
if idx == 81:
return True
i, j = idx // 9, idx % 9
if board[i][j] == '.':
for n in num:
if self.check(board, i, j, n):
board[i][j] = n
if not self.move(board, idx+1):
board[i][j] = '.'
else:
return True
return False
else:
if not self.move(board, idx+1):
return False
def solveSudoku(self, board):
"""
Do not return anything, modify board in-place instead.
"""
self.move(board, 0)
# @lc code=end
# %%
s = Solution()
a = [["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]]
s.solveSudoku(a)
# %%