我試圖用一種我自己製作的算法從頭開始編寫一個基本的數獨解算器,其中我的程序解決了一些空的元素,但不是全部。無論它解決什麼元素,我只是複製那個sudoku板來替換舊的板子,並再次運行我的程序,它應該解決其他空白元素。但是,我與我的程序有關的問題是,元素不會被替換或解決,而我的程序不會打印整個板子。它只打印最後一行。我認爲這與我如何閱讀與羅恩,錐和骨的行,列和塊有關,但我不知道我在這裏做錯了什麼。製作我自己的數獨謎題
這是我讓我追隨的算法。 here
Boards file。
1 4 0 0 8 0 0 3 9
0 3 0 0 0 0 0 1 2
0 0 0 1 0 0 0 0 4
6 7 9 2 5 8 1 4 3
4 0 0 0 9 1 0 0 0
2 0 1 0 0 4 0 6 5
9 0 0 0 1 3 0 0 0
5 1 0 0 0 0 0 9 0
3 6 0 0 4 0 0 8 0
計劃
a1 = []
a2 = []
a3 = []
a4 = []
a5 = []
a6 = []
a7 = []
a8 = []
a9 = []
with open('Boards') as f:
for line in f:
data = line.split()
a1.append(int(data[0]))
a2.append(int(data[1]))
a3.append(int(data[2]))
a4.append(int(data[3]))
a5.append(int(data[4]))
a6.append(int(data[5]))
a7.append(int(data[6]))
a8.append(int(data[7]))
a9.append(int(data[8]))
print("Old Board")
print(a1[0], a2[0], a3[0],"|", a4[0], a5[0], a6[0],"|", a7[0], a8[0], a9[0])
print(a1[1], a2[1], a3[1],"|", a4[1], a5[1], a6[1],"|", a7[1], a8[1], a9[1])
print(a1[2], a2[2], a3[2],"|", a4[2], a5[2], a6[2],"|", a7[2], a8[2], a9[2])
print("---------------------")
print(a1[3], a2[3], a3[3],"|", a4[3], a5[3], a6[3],"|", a7[3], a8[3], a9[3])
print(a1[4], a2[4], a3[4],"|", a4[4], a5[4], a6[4],"|", a7[4], a8[4], a9[4])
print(a1[5], a2[5], a3[5],"|", a4[5], a5[5], a6[5],"|", a7[5], a8[5], a9[5])
print("---------------------")
print(a1[6], a2[6], a3[6],"|", a4[6], a5[6], a6[6],"|", a7[6], a8[6], a9[6])
print(a1[7], a2[7], a3[7],"|", a4[7], a5[7], a6[7],"|", a7[7], a8[7], a9[7])
print(a1[8], a2[8], a3[8],"|", a4[8], a5[8], a6[8],"|", a7[8], a8[8], a9[8])
#Rows
r1 = [a1[0], a2[0], a3[0], a4[0], a5[0], a6[0], a7[0], a8[0], a9[0]]
r2 = [a1[1], a2[1], a3[1], a4[1], a5[1], a6[1], a7[1], a8[1], a9[1]]
r3 = [a1[2], a2[2], a3[2], a4[2], a5[2], a6[2], a7[2], a8[2], a9[2]]
r4 = [a1[3], a2[3], a3[3], a4[3], a5[3], a6[3], a7[3], a8[3], a9[3]]
r5 = [a1[4], a2[4], a3[4], a4[4], a5[4], a6[4], a7[4], a8[4], a9[4]]
r6 = [a1[5], a2[5], a3[5], a4[5], a5[5], a6[5], a7[5], a8[5], a9[5]]
r7 = [a1[6], a2[6], a3[6], a4[6], a5[6], a6[6], a7[6], a8[6], a9[6]]
r8 = [a1[7], a2[7], a3[7], a4[7], a5[7], a6[7], a7[7], a8[7], a9[7]]
r9 = [a1[8], a2[8], a3[8], a4[8], a5[8], a6[8], a7[8], a8[8], a9[8]]
#Cols
c1 = [a1[0], a1[1], a1[2], a1[3], a1[4], a1[5], a1[6], a1[7], a1[8]]
c2 = [a2[0], a2[1], a2[2], a2[3], a2[4], a2[5], a2[6], a2[7], a2[8]]
c3 = [a3[0], a3[1], a3[2], a3[3], a3[4], a3[5], a3[6], a3[7], a3[8]]
c4 = [a4[0], a4[1], a4[2], a4[3], a4[4], a4[5], a4[6], a4[7], a4[8]]
c5 = [a5[0], a5[1], a5[2], a5[3], a5[4], a5[5], a5[6], a5[7], a5[8]]
c6 = [a6[0], a6[1], a6[2], a6[3], a6[4], a6[5], a6[6], a6[7], a6[8]]
c7 = [a7[0], a7[1], a7[2], a7[3], a7[4], a7[5], a7[6], a7[7], a7[8]]
c8 = [a8[0], a8[1], a8[2], a8[3], a8[4], a8[5], a8[6], a8[7], a8[8]]
c9 = [a9[0], a9[1], a9[2], a9[3], a9[4], a9[5], a9[6], a9[7], a9[8]]
#3x3 boxes from Left to right
b1 = [a1[0], a2[0], a3[0], a1[1], a2[1], a3[1], a1[2], a2[2], a3[2]]
b2 = [a4[0], a5[0], a6[0], a4[1], a5[1], a6[1], a4[2], a5[2], a6[2]]
b3 = [a7[0], a8[0], a9[0], a7[1], a8[1], a9[1], a7[2], a8[2], a9[2]]
b4 = [a1[3], a2[3], a3[3], a1[4], a2[4], a3[4], a1[5], a2[5], a3[5]]
b5 = [a4[3], a5[3], a6[3], a4[4], a5[4], a6[4], a4[5], a5[5], a6[5]]
b6 = [a7[3], a8[3], a9[3], a7[4], a8[4], a9[4], a7[5], a8[5], a9[5]]
b7 = [a1[6], a2[6], a3[6], a1[7], a2[7], a3[7], a1[8], a2[8], a3[8]]
b8 = [a4[6], a5[6], a6[6], a4[7], a5[7], a6[7], a4[8], a5[8], a6[8]]
b9 = [a7[6], a8[6], a9[6], a7[7], a8[7], a9[7], a7[8], a8[8], a9[8]]
print("\n")
#Start with rows 1-9
count = 1
for rone in r1, r2, r3, r4, r5, r6, r7, r8, r9: #Checks the rows initially 1-9
#Check elements 1-9 for a 0
for element in rone:
if element == 0:
#Count represents the number that is being looked for
while count !=9:
#Reset Option (Option=0)
Option = 0
#Check Block
for bone in b1, b2, b3, b4, b5, b6, b7, b8, b9: #Check block for number
for element in bone:
#If there is an element 1-9 when checking 3x3 block that the selected element is in break out of loop and
#increment count to next number to check for
if element == count: #supposed element in block already
count += 1 #++count
break
else:
Option += 1
#Check Row
for rone in r1, r2, r3, r4, r5, r6, r7, r8, r9: #Check Row for number
for element in rone:
#If there is an element 1-9 when checking row that the selected element is in break out of loop and
#increment count to next number to check for
if element == count: #supposed elemnet in row already
count += 1
break #or Pass or break
else:
Option += 1
for cone in c1, c2, c3, c4, c5, c6, c7, c8, c9: #Check column for number
for element in cone:
#If there is an element 1-9 when columnthat the selected element is in break out of loop and
#increment count to next number to check for
if element == count: #supposed elemnet in column already
count += 1
break
else:
#The number is not in the row, column, or block so theres an option
Option += 1
break
if Option == 1:
#Place number in elemnent
#Replace element with count
count = element
count = 0
else:
#Else more than one option, loop to count and ++count
count += 1
break
count += 1
break
continue
print("New Board", rone)
你不能指望從頭開始編寫一個謎題解算器,並立即開始工作。在編寫代碼時將打印語句添加到代碼中,進行小的更改並確保它們正常工作。這樣,當你來到Stack Overflow時,你將會得到一個非常具體的問題和更少的代碼,我們需要通過它來回答它。 –
另外,最好使用嵌套列表,而不是使用多組九個變量,每個變量都包含一個列表。 –