2016-05-28 3 views
0

Ich mache ein NQueen Problem Solver aber jedes Mal, wenn ich das Programm ausführen es diesen Fehler starten:NQueens Slver in Python

„Tupel“ hat kein Attribut „Reihe“

Ich möchte wissen, ob die Funktion Platz ist gut, weil Struktur ist derjenige, der den Fehler

import sys #use of argv 

#Create the object queen with the row and column 
class Queen: 
    def __init__(self, row, col): 
     self.row = row 
     self.col = col 

    def __repr__(self): 
     return "Row: "+ str(self.row) + " Column: " + str(self.col) 

#Validate if I can place the Queen in that space, return false or true 
def place(Board,row,col): 
    for i in range(0,len(Board)-1): 
     #Attack conditions: cant place in the same column, row and diagonal 
     if (Board[i].row == row) or (Board[i].col == col) or (abs(Board[i].row - row) == abs(Board[i].col - col)): 
      return False 
    return True 

#Recursive function that Append the queen to the board if it can be place 
def NQueens(Board,n,row): 
    for i in range(1,n): 
     #if the row is > than n the fuction will return the list 
     if (row > n): 
      return Board 
     #If it can be place we call the NQueens function with the next row 
     elif place(Board,row,i): 
      Queenaux = (row,i) 
      Board.append(Queenaux) 
      NQueens(Board,n,row+1) 

#The user enter the number of queens and size of the board 
n = int(sys.argv[1]) 
print("nQueens Problem Solver") 
print("Chess Board: ",n,"x",n) 
print("Number Queens:",n) 
#Create the list Board 
Board = [] 
#Start the NQueens function with row=1 
Board = NQueens(Board,n,1) 
#Print the queens in the board and their coordinates 
print (Board) 
+0

Was ist die Variable "Queens"? –

+0

Sorry dude, Queens = Board, ich habe es bereits geändert aber es funktionierte nicht der gleiche Fehler –

Antwort

0
Queenaux = (row,i) 
Board.append(Queenaux) 

aufgrund dieser Linien werfen, Board Tupel enthält. Sie müssen es beheben, indem Queenaux eine Queen Instanz machen, etwa so:

Queenaux = Queen(row,i) 

Beachten Sie auch, dass NQueens nichts zurückliefert, so dass Sie es zu Board nicht zuweisen sollte. Rufen Sie einfach die Funktion an und lassen Sie sie die Änderungen vornehmen.

+0

'NQueens' manchmal gibt etwas zurück. Es ist inkonsistent. Dies ist eines von mehreren strukturellen Problemen. Während der obige Fix den ursprünglichen Fehler behebt, wird der Algorithmus nicht korrekt. Hier gibt es viele Probleme. Hast du über Rohls 2 Algorithmus gelesen? – RobertB