-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2239.py
54 lines (43 loc) ยท 944 Bytes
/
2239.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
import sys
input = sys.stdin.readline
arr = [list(map(int, input().rstrip())) for _ in range(9)]
def check_row(i, x, y):
for j in range(9):
if arr[x][j] == i:
return False
return True
def check_col(i, x, y):
for j in range(9):
if arr[j][y] == i:
return False
return True
def check_3x3(i, x, y):
for m in range(x // 3 * 3, x // 3 * 3 + 3):
for n in range(y // 3 * 3, y // 3 * 3 + 3):
if arr[m][n] == i:
return False
return True
flag = False
def track(n):
global flag
x = n // 9
y = n % 9
if flag:
return
if n == 81:
flag = True
for i in range(9):
for j in range(9):
print(arr[i][j], end = '')
print()
return
if arr[x][y] != 0:
track(n + 1)
else:
for i in range(1, 10):
if check_row(i, x, y) and check_col(i, x, y) and check_3x3(i, x, y):
arr[x][y] = i
track(n + 1)
arr[x][y] = 0
return
track(0)