-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.py
86 lines (69 loc) · 2 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_solved(data):
for row in range(9):
for col in range(9):
if data[row][col] == 0:
return False, row, col
return True, 0, 0
def check_number_exists(data, row, col, number):
for i in range(9):
if data[row][i] == number or data[i][col] == number:
return True
if row <= 2:
x1 = 0
x2 = 2
elif row <= 5:
x1 = 3
x2 = 5
else:
x1 = 6
x2 = 8
if col <= 2:
y1 = 0
y2 = 2
elif col <= 5:
y1 = 3
y2 = 5
else:
y1 = 6
y2 = 8
for l in range(x1, x2 + 1):
for k in range(y1, y2 + 1):
if data[l][k] == number:
return True
return False
def solve_puzzle(data):
solved, row, col = check_solved(data)
if solved:
return True
for number in range(1, 10):
if not check_number_exists(data, row, col, number):
data[row][col] = number
if solve_puzzle(data):
return True
data[row][col] = 0
return False
def print_puzzle(data):
for row in range(9):
if row % 3 == 0:
print("------------------------------")
for col in range(9):
print(" " + str(data[row][col]) + " ", end='')
if (col + 1) % 3 == 0:
print("|", end='')
print("")
print("------------------------------", end='')
if __name__ == "__main__":
input = [[0, 3, 0, 7, 4, 0, 0, 0, 0],
[0, 6, 0, 0, 0, 2, 1, 0, 0],
[0, 0, 0, 0, 0, 8, 0, 3, 0],
[8, 0, 3, 0, 0, 0, 0, 0, 0],
[9, 1, 0, 0, 0, 0, 0, 5, 4],
[0, 0, 0, 0, 0, 0, 3, 0, 2],
[0, 5, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 6, 9, 0, 0, 0, 7, 0],
[0, 0, 0, 0, 5, 4, 0, 8, 0]]
print("\nPuzzle:")
print_puzzle(input)
solve_puzzle(input)
print("\nSolution:")
print_puzzle(input)