-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewDancesolver.py
217 lines (159 loc) · 5.49 KB
/
newDancesolver.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import time
from config import activeConfig as con
DLCont = True
timetosleep = 0.1
stepcount = 0
class DLHeaderNode:
def __init__(self, up, down, left, right, size):
self.up = up
self.down = down
self.left = left
self.right = right
self.size = size
class DLNode:
def __init__(self, up, down, left, right, header, rowNumber):
self.up = up
self.down = down
self.left = left
self.right = right
self.header = header
self.rowNumber = rowNumber
def dancingLinks(matrix):
numRows = 729
numCols = 324
masterNode = DLHeaderNode(None, None, None, None, -1)
topColumnNodeList = []
topRowNodeList = []
prevCol = masterNode
for c in range(numCols):
currentCol = DLHeaderNode(None, None, prevCol, None, 0)
topColumnNodeList.append(currentCol)
prevCol.right = currentCol
prevCol = currentCol
topColumnNodeList[len(topColumnNodeList)-1].right = masterNode
masterNode.left = topColumnNodeList[len(topColumnNodeList)-1]
for i in range(numRows):
r = i // 81
c = (i%81)//9
num = i % 9 + 1
node1Index = r*9 + c
node2Index = 80 + 9*r + num
node3Index = 161 + 9*c + num
b = (r//3)*3 + c//3
node4Index = 242 + 9*b + num
node1 = DLNode(None, None, None, None, topColumnNodeList[node1Index], i)
node2 = DLNode(None, None, node1, None, topColumnNodeList[node2Index], i)
node3 = DLNode(None, None, node2, None, topColumnNodeList[node3Index], i)
node4 = DLNode(None, None, node3, None, topColumnNodeList[node4Index], i)
node1.right = node2
node2.right = node3
node3.right = node4
topRowNodeList.append(node1)
node4.right = node1
node1.left = node4
topCol1 = topColumnNodeList[node1Index]
topCol2 = topColumnNodeList[node2Index]
topCol3 = topColumnNodeList[node3Index]
topCol4 = topColumnNodeList[node4Index]
addNodeToBottomOfAColumn(node1, topCol1)
addNodeToBottomOfAColumn(node2, topCol2)
addNodeToBottomOfAColumn(node3, topCol3)
addNodeToBottomOfAColumn(node4, topCol4)
topColumnNodeList[node1Index].size +=1
topColumnNodeList[node2Index].size +=1
topColumnNodeList[node3Index].size +=1
topColumnNodeList[node4Index].size +=1
for i in range(len(matrix)):
for j in range(len(matrix)):
if matrix[i][j]!=0:
num = matrix[i][j]
rowNumber = (num-1) + i*81 +j*9
headNodeFromARow = topRowNodeList[rowNumber]
coverColumn(headNodeFromARow.header)
temp = headNodeFromARow.right
while temp != headNodeFromARow:
coverColumn(temp.header)
temp = temp.right
global DLCont
DLCont = True
DLSearch(0, masterNode, matrix)
def DLSearch(k, masterNode, matrix):
global DLCont, stepcount
if not DLCont:
return
if masterNode.right == masterNode:
DLCont = False
print(stepcount)
return
tempC = masterNode.right
c = tempC
while tempC != masterNode:
if c.size > tempC.size:
c = tempC
tempC = tempC.right
coverColumn(c)
r = c.down
while r != c:
if not DLCont:
return
correspondingSudokuBoardRow = r.rowNumber //81
correspondingSudokuBoardColumn = (r.rowNumber%81)//9
correspondingSudokuBoardNumber = r.rowNumber % 9 +1
stepcount += 1
matrix[correspondingSudokuBoardRow][correspondingSudokuBoardColumn] = correspondingSudokuBoardNumber
time.sleep(con.sleeptime)
# row = correspondingSudokuBoardRow
# col = correspondingSudokuBoardColumn
# i = correspondingSudokuBoardNumber
j = r.right
while j != r:
coverColumn(j.header)
j = j.right
DLSearch(k+1, masterNode, matrix)
if not DLCont:
return
matrix[correspondingSudokuBoardRow][correspondingSudokuBoardColumn] = 0
j = r.left
while j != r:
uncoverColumn(j.header)
j = j.left
r = r.down
uncoverColumn(c)
def addNodeToBottomOfAColumn(nodeToAdd, columnTop):
temp = columnTop
while temp.down is not None and temp.down != columnTop:
temp = temp.down
temp.down = nodeToAdd
nodeToAdd.up = temp
nodeToAdd.down = columnTop
columnTop.up = nodeToAdd
def coverColumn(topColumnNode):
c = topColumnNode
c.right.left = c.left
c.left.right = c.right
i = c.down
while i!= c:
j = i.right
while j != i:
j.down.up = j.up
j.up.down = j.down
j.header.size -=1
j = j.right
i = i.down
def uncoverColumn(topColumnNode):
c = topColumnNode
i = c.up
while i != c:
j = i.left
while j != i:
j.header.size +=1
j.down.up = j
j.up.down = j
j = j.K_LEFT
i = i.up
c.right.left = c
c.left.right = c
if __name__ == "__main__":
testgrid = [[3, 4, 0, 0, 1, 0, 9, 0, 0], [0, 0, 1, 0, 0, 4, 0, 8, 3], [5, 0, 0, 0, 0, 0, 0, 1, 0], [9, 1, 0, 0, 5, 0, 0, 0, 0], [0, 6, 4, 0, 0, 0, 1, 3, 0], [0, 0, 0, 0, 8, 0, 0, 4, 9], [0, 8, 0, 0, 0, 0, 0, 0, 2], [2, 3, 0, 9, 0, 0, 4, 0, 0], [0, 0, 9, 0, 4, 0, 0, 5, 8]]
dancingLinks(testgrid)
print(testgrid)