# Solving the NQueens problem in R, but I keep getting the "argument is of length 0" and "condition has length >1" errors

I've been working on solving the NQueens problem in R. I want to create an 8 by 8 2D array filled with 0s. Then I created a recursive algorithm to trace where I can safely put a queen in each row. Unfortunately, I keep running into the "argument is of length 0 error" and a warning of "condition has length >1." Any help would be appreciated!

''' r
checkOpen <- function(board, row, col){
for(i in col){
if(board[row,i,2] == 1){
return(FALSE)
}
}
i <- row
j <- col
while(i > 0 && j > 0){
if (board[i,j,2] == 1){
return(FALSE)
}
i <- i-1
j <- j-1
}
i <- row
j <- col
while(i < row && j > 0){
if(board[i,j,2] == 1){
return(FALSE)
}
}
return(TRUE)
}

placeQueenRecursive <- function(board, column){
if(column >= col_global){
return(TRUE)
}
for(i in row_global){
if(checkOpen(board, i, column)){
board[i,column,] <- 1
if(placeQueenRecursive(board, column+1) == TRUE){
return(TRUE)}
board[i,column,] <- 0}
}
}

placeQueen <- function(board,queens){
if(placeQueenRecursive(board, 0) == False){
print("No Solution")
return(FALSE)
}
print(board)
return(TRUE)
}

queens <<- 0
row_global <<- c(1,2,3,4,5,6,7,8)
col_global <<- c(1,2,3,4,5,6,7,8)

empty <- c(0,0,0,0,0,0,0,0)

board <<- array(c(empty, empty, empty, empty, empty, empty, empty, empty), dim = c(8,8,2))
board[1,2,2]

placeQueen(board,queens)
'''

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.