(* Helper function to check if a queen can be placed at (row, col) *)
isSafe[board_, row_, col_] :=
And @@ (And[# != row, # + board[[#]] != row + col, # - board[[#]] != row - col] & /@ Range[col - 1]);
(* Recursive function to solve the 9-queens problem *)
solveQueens[board_, col_] :=
If[col > 9, (* If all queens are placed, return the solution *)
Print[board];,
(* Else, try placing the queen in each row of the current column *)
Do[
If[isSafe[board, row, col],
solveQueens[ReplacePart[board, col -> row], col + 1]; (* Recur for the next column *)
],
{row, 1, 9}
]
];