Learn
Exam

N Queen Problem

Difficulty
INTERMEDIATE

Title

N Queen Problem


Problem

Given the size of a chessboard and the number of Queens n as an input parameter, write a solution function that returns the number of possible ways to place n Queens on an n ×\times n chessboard such that no two Queens attack each other.

Background Information

  • The N-Queen problem is a classical problem in which n Queens are placed on an n ×\times n chessboard such that no two Queens attack each other.
  • A Queen can attack any other piece in all vertical, horizontal, and diagonal directions, regardless of distance.

Constraints

  • n is a natural number between 3 and 12, inclusive.

Test Case Explanation

Test Case Number n return
1 3 0
2 5 10
3 8 92

Explanation for Test Case 1

  • For a 3 ×\times 3 chessboard, it is impossible to place 3 Queens such that no two Queens attack each other. Thus, the result is 0.

Explanation for Test Case 2

  • For a 5 ×\times 5 chessboard, there are 10 ways to place 5 Queens such that no two Queens attack each other.
  • One of these arrangements is shown below:

Explanation for Test Case 3

  • For an 8 ×\times 8 chessboard, there are 92 ways to place 8 Queens such that no two Queens attack each other.
  • One of these arrangements is shown below: