backtracking - NQueens Slver in python -
i making nqueen problem solver every time run program launch error:
"tuple" has no attribute "row"
i want know if function place structure because 1 throw error
import sys #use of argv #create object queen row , 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 can place queen in space, return false or true def place(board,row,col): in range(0,len(board)-1): #attack conditions: cant place in same column, row , 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 append queen board if can place def nqueens(board,n,row): in range(1,n): #if row > n fuction return list if (row > n): return board #if can place call nqueens function next row elif place(board,row,i): queenaux = (row,i) board.append(queenaux) nqueens(board,n,row+1) #the user enter number of queens , size of board n = int(sys.argv[1]) print("nqueens problem solver") print("chess board: ",n,"x",n) print("number queens:",n) #create list board board = [] #start nqueens function row=1 board = nqueens(board,n,1) #print queens in board , coordinates print (board)
queenaux = (row,i) board.append(queenaux)
because of these lines, board
contains tuples. must fix making queenaux
queen
instance, so:
queenaux = queen(row,i)
also note nqueens
doesn't return shouldn't assign board
. call function , let modifications.
Comments
Post a Comment