-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.py
86 lines (70 loc) · 1.99 KB
/
sudoku.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def check_row(x, y):
check = [False] * 10
for number in a[x]:
if check[number] and number == a[x][y]:
return False
check[number] = True
return True
def check_col(x, y):
col = [a[j][y] for j in range(9)]
check = [False] * 10
for number in col:
if check[number] and number == a[x][y]:
return False
check[number] = True
return True
def check_square(x, y):
check = [False] * 10
seq = (x // 3) * 3 + (y // 3)
for i in range(3):
r = i + (seq // 3) * 3
for j in range(3):
c = j + (seq % 3) * 3
if check[a[r][c]] and a[r][c] == a[x][y]:
return False
check[a[r][c]] = True
return True
def find_answer(x, y):
row = a[x]
col = [a[j][y] for j in range(9)]
first = []
for i in range(1, 10):
if i not in row and i != a[x][y]:
first.append(i)
second = []
for i in range(1, 10):
if i not in col and i != a[x][y]:
second.append(i)
if len(first) == 1:
return first[0]
if len(second) == 1:
return second[0]
def check_correct(ans, x, y):
temp = a[x][y]
a[x][y] = ans
if check_row(x, y) and check_col(x, y) and check_square(x, y):
a[x][y] = temp
return True
a[x][y] = temp
return False
def find_bug():
for z in range(81):
x, y = z // 9, z % 9
if not check_row(x, y) and not check_col(x, y) and not check_square(x, y):
ans = find_answer(x, y)
if check_correct(ans, x, y):
answer.append(x + 1)
answer.append(y + 1)
answer.append(ans)
if len(answer) == k * 3:
return
num_of_test = int(input())
for test in range(1, num_of_test + 1):
k = int(input())
a = [list(map(int, input().split())) for _ in range(9)]
answer = []
find_bug()
print("#%d" % test, end=" ")
for x in answer:
print(x, end=" ")
print()