2016-02-01 67 views
-3

我試圖用一種我自己製作的算法從頭開始編寫一個基本的數獨解算器,其中我的程序解決了一些空的元素,但不是全部。無論它解決什麼元素,我只是複製那個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) 
+0

你不能指望從頭開始編寫一個謎題解算器,並立即開始工作。在編寫代碼時將打印語句添加到代碼中,進行小的更改並確保它們正常工作。這樣,當你來到Stack Overflow時,你將會得到一個非常具體的問題和更少的代碼,我們需要通過它來回答它。 –

+0

另外,最好使用嵌套列表,而不是使用多組九個變量,每個變量都包含一個列表。 –

回答

0

它打印只有一排,因爲你明確告訴它打印只有最後一排,一旦你與整個外環完成。如果縮進的語句來得到它羅內循環內,你會看到在你對待他們的順序打印的行:

Old Board 
(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) 


('New Board', [3, 6, 0, 0, 4, 0, 0, 8, 0]) 
('New Board', [0, 3, 0, 0, 0, 0, 0, 1, 2]) 
('New Board', [0, 0, 0, 1, 0, 0, 0, 0, 4]) 
('New Board', [6, 7, 9, 2, 5, 8, 1, 4, 3]) 
('New Board', [4, 0, 0, 0, 9, 1, 0, 0, 0]) 
('New Board', [2, 0, 1, 0, 0, 4, 0, 6, 5]) 
('New Board', [9, 0, 0, 0, 1, 3, 0, 0, 0]) 
('New Board', [5, 1, 0, 0, 0, 0, 0, 9, 0]) 
('New Board', [3, 6, 0, 0, 4, 0, 0, 8, 0]) 

這解決了你的眼前問題。但是,在這之後你會遇到更多問題。最明顯的是,在進入下一個代碼之前,你沒有得到代碼的一部分 - 你似乎已經在一個大塊中編寫了代碼,這使得調試更加困難。首先,你可以在循環內改變rone循環:你有一個嵌套循環,它改變了rone,在其他子句的第102行。這嚴重干擾了你最外層循環的循環流。

接下來,繼續遍歷原始主板:b1-b9,r1-r9和c1-c9。據我所見,你根本不會更新這些,因爲你解決了這個難題。

總體而言,您似乎已經超出了您的編程能力與一個更大的項目。就列表而言,您還沒有認爲,並且您沒有從基本數據結構的角度思考組織任務。我強烈建議你首先在一些小型項目上工作,然後在你再一次行使這些基本技能時再回到這個項目。不要把它扔掉 - 這是一個很棒的學習體驗。我保留了我的一流課程,並且在學習一門新語言時仍然回到他們身邊。事實上,我將它們分配給我自己的學生,並且經常向他們展示我所犯的錯誤(在給他們時間找出他們自己的錯誤之後)。

如果您正在尋找需要解決的問題,您可以從Project Euler中開始。這是一個非常可愛的算法謎題集合,每週都會發佈一個新問題。你會建立一些軟件工具...其中一個問題是一個不錯的數獨求解器。

1

首先,我認爲在每次迭代之後用一個新板代替舊板會太「昂貴」,並且通常不是很好的編程實踐。

下面,我已經提供了一個基本的算法,你可以用它來解決9×9數獨板。做你會用它的。'

Disp 81 emtpy square grids; 
Fill grids with some known numbers; 

while(there are empty square){ 
    if(top left square is empty){ 
     create a number x; 
     if(no other numbers x are in row/column/mini grid){ 
      plug in x; 
     } 
    } else{ 
     while(row number < 9){ 
      go to next row; 
      if(row number == 9){ 
       row number = 1; 
       next column; 
      } 
     } 
    } 
} 

我意識到我提供給你的算法可能有點基本,但它是一個開始。