I’m trying to write a short Julia program to solve the n queens problem – where you try place n queens on an nxn chess board such that they don’t attack each other – I am new to Julia and I don’t understand why the error “MethodError: no method matching check(::Int64, ::Int64, ::Int64, ::BitMatrix)” is given when check is defined with argument types for the variable types desired.
I’ve tried to detail the variable types more within the function and prior to passing the values, but it still tries to find a method with different data types.
code listed below
solve(1, 6, falses(6, 6))
display(board)
function check(row::Int16, column::Int16, n::Int16, board::BitMatrix)
left::Int16 = 0
checkLeft::Bool = true
right::Int16 = 0
checkRight::Bool = true
for j::Int16 = row:-1:1
if left==column-1
checkLeft = false
else
left+=1
end
if right==n-column
checkRight = false
else
right+=1
end
if board[j,column]||(board[j,column-left]&&checkLeft)||(board[j,column+right]&&checkRight)
return true
end
end
return false
end
function solve(row::Int16, n::Int16, board::BitMatrix)
column::Int16 = 1
if row>=n
return true
end
for column = 1:n
if check(row, column, n, board)
board[row,column] = true
if solve(row+1, n, board)
return true
end
board[row,column] = false
else
column+=1
end
end
return false
end
Another Person is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.