Yes, it's correct. So, I completed the code using this method:
ruby method using a hash of positional arrays:
# Queen movement arrays as 1D arrays (Ruby hash)
queen_moves = {
'Position 0' => [1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
'Position 1' => [1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0],
'Position 2' => [1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0],
'Position 3' => [1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1],
'Position 4' => [1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0],
'Position 5' => [1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1],
'Position 6' => [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0],
'Position 7' => [0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1],
'Position 8' => [1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
'Position 9' => [0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0],
'Position 10' => [1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
'Position 11' => [0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
'Position 12' => [1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1],
'Position 13' => [0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1],
'Position 14' => [0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
'Position 15' => [1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1]
}
# Define a function to check if placing a queen at a certain position is safe
def is_safe(position, queens)
row, col = position / 4, position % 4
queens.each do |q|
q_row, q_col = q / 4, q % 4
# Check for conflicts in the same row, column, and diagonals
return false if q_row == row || q_col == col || (q_row - row).abs == (q_col - col).abs
end
true
end
# Recursive function to solve the 4-queens problem
def solve_4_queens(queens = [], row = 0, solutions = [])
if queens.size == 4 # All queens placed
solutions << queens.dup # Store the solution
return
end
(0..15).each do |position|
next unless position / 4 == row # Ensure we place one queen per row
if is_safe(position, queens)
queens.push(position) # Place the queen
solve_4_queens(queens, row + 1, solutions)
queens.pop # Backtrack
end
end
solutions
end
# Call the function to solve the 4-queens problem
solutions = solve_4_queens
# Print all the solutions
if solutions.empty?
puts "No solutions found"
else
solutions.each_with_index do |solution, index|
puts "Solution #{index + 1}:"
solution.each do |pos|
puts "Queen placed at: Position #{pos} (Row #{pos / 4}, Column #{pos % 4})"
end
puts "\n"
end
end
run it:
macos $ ruby four_queens.rb
Solution 1:
Queen placed at: Position 1 (Row 0, Column 1)
Queen placed at: Position 7 (Row 1, Column 3)
Queen placed at: Position 8 (Row 2, Column 0)
Queen placed at: Position 14 (Row 3, Column 2)
Solution 2:
Queen placed at: Position 2 (Row 0, Column 2)
Queen placed at: Position 4 (Row 1, Column 0)
Queen placed at: Position 11 (Row 2, Column 3)
Queen placed at: Position 13 (Row 3, Column 1)
ruby four_queens.rb 0.03s user 0.02s system 80% cpu 0.064 total
So, that was kinda fun. I used ChatGPT 4o to generate all 16 positional arrays (finally) and then worked with ChatGPT back-and-forth a bit more to write the Ruby code (as I wanted it to be) to solve this for both solutions.
Also, I did a quick network search before going down this little rabbit hole, and did not find any solution similar to this one; but I probably missed something as I did not search very long; only looking for about 10 minutes.