-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktracking.py
120 lines (109 loc) · 3.95 KB
/
backtracking.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
import copy
staticList = [[0, 0, 8, 6, 3, 2, 0, 0, 1],
[0, 1, 0, 0, 4, 5, 0, 0, 7],
[0, 0, 2, 0, 9, 1, 5, 0, 0],
[6, 0, 0, 0, 7, 8, 0, 2, 0],
[0, 0, 7, 0, 0, 0, 8, 0, 0],
[0, 9, 0, 1, 2, 0, 0, 0, 5],
[0, 0, 3, 5, 8, 0, 6, 0, 0],
[5, 0, 0, 2, 1, 0, 0, 7, 0],
[2, 0, 0, 9, 6, 4, 3, 0, 0]]
for i in range(9):
try:
firstIndex = [i,staticList[i].index(0)]
break
except:
pass
validEntry = False
activeList = copy.deepcopy(staticList)
activeIndex = list.copy(firstIndex)
activeSearch = 1
lastActiveIndex = list.copy(firstIndex)
def boxSearch(firstIndex,searchValue,boardList):
topLeftRow = 0
topLeftColumn = 0
#determines top left row and column of current index square
if firstIndex[0] >= 0 and firstIndex[0] <= 2:
topLeftRow = 0
elif firstIndex[0] >= 3 and firstIndex[0] <= 5:
topLeftRow = 3
else:
topLeftRow = 6
if firstIndex[1] >= 0 and firstIndex[1] <= 2:
topLeftColumn = 0
elif firstIndex[1] >= 3 and firstIndex[1] <= 5:
topLeftColumn = 3
else:
topLeftColumn = 6
for i in range(3):
for j in range(3):
if boardList[topLeftRow][topLeftColumn + i] == searchValue:
return False
return(True)
def columnSearch(firstIndex,searchValue,boardList):
for row in boardList:
if row[firstIndex[1]] == searchValue:
return False
return True
def rowSearch(firstIndex,searchValue,boardList):
for number in boardList[firstIndex[0]]:
if number == searchValue:
return False
return True
def findEmptyIndex(staticList,currentPoint):
foundIndex = False
currentPoint = changeCurrentIndex(currentPoint,False)
while not foundIndex:
if staticList[currentPoint[0]][currentPoint[1]] != 0:
currentPoint = changeCurrentIndex(currentPoint,False)
else:
return currentPoint
def changeCurrentIndex(currentPoint,isDown = True):
if isDown:
if currentPoint[1] == 8:
currentPoint[0] += 1
currentPoint[1] = 0
else:
currentPoint[1] += 1
else:
if currentPoint[1] == 0:
currentPoint[0] -= 1
currentPoint[1] = 8
else:
currentPoint[1] -= 1
return(currentPoint)
while validEntry == False:
#if the current cell is not zero, then go back one cell and continue until it is 0
while staticList[activeIndex[0]][activeIndex[1]] != 0:
activeIndex = changeCurrentIndex(activeIndex)
#once we have a cell that was orginally empty, and can be changed
#Search the activeIndex at the current search value and evaluate if it is valid on the board.
if not(boxSearch(activeIndex,activeSearch,activeList)) or not(rowSearch(activeIndex,activeSearch,activeList)) or not((columnSearch(activeIndex,activeSearch,activeList))):
#increase the search value if less than 9, and resets current index and goes back one
if activeSearch == 9:
activeList[activeIndex[0]][activeIndex[1]] = 0
activeIndex = findEmptyIndex(staticList,activeIndex)
while(activeList[activeIndex[0]][activeIndex[1]] == 9):
activeList[activeIndex[0]][activeIndex[1]] = 0
activeIndex = findEmptyIndex(staticList,activeIndex)
activeSearch = activeList[activeIndex[0]][activeIndex[1]] + 1
else:
activeSearch += 1
#Current search term works!
else:
activeList[activeIndex[0]][activeIndex[1]] = activeSearch
activeIndex = changeCurrentIndex(activeIndex)
activeSearch = 1
invalid = False
for row in activeList:
for column in row:
if column == 0:
invalid = True
break
if invalid:
break
if not invalid:
validEntry = True
for each in activeList:
print(each)
print()