N-Queens solution using naive recursion.
We know all queens must be on distinct columns. Hence for each column recursively attempt each row, terminating if the current configuration with the current subset of columns is invalid.
Validity checking does not need to check if any columns overlap (by definition they do not); we only check if both conditions are false:
- any rows overlap,
placement[i] == placement[j]
, and - any queen lies on the same diagonal as another,
Math.abs(i - j) == Math.abs(placement[i] - placement[j])
This is not the worst solution (the worst involves 64 choose 8 ~= 4 * 10^9!!), but not the best (a DFS graph search would do better). Indeed results show this code is too inefficient above n=14.