N-Queens Problem

Expert

The N-Queens Problem is a classic puzzle involving placing N queens on an N x N chessboard such that no two queens threaten each other. This means no two queens can be in the same row, column, or diagonal.

Your task is to write a function n_queens(n: int) -> int that returns the number of distinct solutions to the N-Queens problem on an N x N chessboard.

Examples:

  • If n = 4, there are 2 possible distinct solutions.
  • If n = 1, the only possible solution is placing the queen on the single cell.

Here are some function calls to guide you:

print(n_queens(4)) # Output: 2 print(n_queens(1)) # Output: 1 print(n_queens(8)) # Output: 92

The challenge involves using concepts like backtracking to systematically search for solutions and ensuring that the queen placement follows the rules of chess. Good luck, and enjoy the challenge!

Geek
Inputs

Arg 1:

4
Expected Output
2
Actual Output
N-Queens Problem [expert] | Coding for Kids