data-structures · intermediate · ~15 min
Place n non-attacking queens by backtracking.
Implement:
int count_n_queens(int n);
Return the number of ways to place n non-attacking queens on an n x n board, using backtracking. 1 <= n <= 12.
n in [1,12].
Number of solutions.
Track used columns and both diagonals.
#include <stddef.h>
/* Number of ways to place n non-attacking queens on an n x n board (backtracking). 1<=n<=12. */
int count_n_queens(int n){ (void)n; return 0; }
Not un-marking state when backtracking; wrong diagonal indices.
n=2 and n=3 have 0 solutions.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.