-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.py
133 lines (99 loc) · 4.02 KB
/
Solution.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
class SudokuSolution:
def __init__(self, board):
self.board = board
self.solve()
def getSolution(self):
return self.board
def findNextCell(self, i, j):
for x in range(i, 9):
for y in range(j, 9):
if self.board[x][y] == 0:
return x, y
for x in range(0, 9):
for y in range(0, 9):
if self.board[x][y] == 0:
return x, y
return -1, -1
def isValid(self, e, i, j):
rowOk = self.checkRows(e, i)
if rowOk:
columnOk = self.checkCols(e, j)
if columnOk:
return self.checkSquares(e, i, j)
return False
def checkCols(self, num, col):
check = []
for x in range(9):
check.append(self.board[x][col])
if num in check:
return False
return True
def checkRows(self, num, row):
if num in self.board[row]:
return False
return True
def checkSquares(self, num, rows, cols):
block = []
if rows % 3 == 0:
if cols % 3 == 0:
block.append(self.board[rows + 1][cols + 1])
block.append(self.board[rows + 1][cols + 2])
block.append(self.board[rows + 2][cols + 1])
block.append(self.board[rows + 2][cols + 2])
elif cols % 3 == 1:
block.append(self.board[rows + 1][cols - 1])
block.append(self.board[rows + 1][cols + 1])
block.append(self.board[rows + 2][cols - 1])
block.append(self.board[rows + 2][cols + 1])
elif cols % 3 == 2:
block.append(self.board[rows + 1][cols - 2])
block.append(self.board[rows + 1][cols - 1])
block.append(self.board[rows + 2][cols - 2])
block.append(self.board[rows + 2][cols - 1])
elif rows % 3 == 1:
if cols % 3 == 0:
block.append(self.board[rows - 1][cols + 1])
block.append(self.board[rows - 1][cols + 2])
block.append(self.board[rows + 1][cols + 1])
block.append(self.board[rows + 1][cols + 2])
elif cols % 3 == 1:
block.append(self.board[rows - 1][cols - 1])
block.append(self.board[rows - 1][cols + 1])
block.append(self.board[rows + 1][cols - 1])
block.append(self.board[rows + 1][cols + 1])
elif cols % 3 == 2:
block.append(self.board[rows - 1][cols - 2])
block.append(self.board[rows - 1][cols - 1])
block.append(self.board[rows + 1][cols - 2])
block.append(self.board[rows + 1][cols - 1])
elif rows % 3 == 2:
if cols % 3 == 0:
block.append(self.board[rows - 2][cols + 1])
block.append(self.board[rows - 2][cols + 2])
block.append(self.board[rows - 1][cols + 1])
block.append(self.board[rows - 1][cols + 2])
elif cols % 3 == 1:
block.append(self.board[rows - 2][cols - 1])
block.append(self.board[rows - 2][cols + 1])
block.append(self.board[rows - 1][cols - 1])
block.append(self.board[rows - 1][cols + 1])
elif cols % 3 == 2:
block.append(self.board[rows - 2][cols - 2])
block.append(self.board[rows - 2][cols - 1])
block.append(self.board[rows - 1][cols - 2])
block.append(self.board[rows - 1][cols - 1])
if num in block:
return False
return True
def solve(self, i=0, j=0):
i, j = self.findNextCell(i, j)
if i == -1:
return True
for x in range(1, 10):
if self.isValid(x, i, j):
self.board[i][j] = x
if self.solve(i, j):
return True
self.board[i][j] = 0
return False