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)
Was ist die Variable "Queens"? –
Sorry dude, Queens = Board, ich habe es bereits geändert aber es funktionierte nicht der gleiche Fehler –