Menu
CoddyTech

N-Queens II

A queen attacks every square in its row, its column and both of its diagonals, however far away. Given an integer n, return the number of ways to place n queens on an n × n board so that no two queens attack each other.

Boards that differ in any square count separately, mirror images included.

Function

totalNQueens(n: integer) → integer
ninteger
the size of the board and the number of queens
Returnsinteger
the number of ways to place the queens so that none attacks another

Constraints

  • 1 ≤ n ≤ 12
  • The answer for n = 12 is 14,200, so it fits in a 32-bit integer.

Examples

Input
n = 4
Output
2
Explanation
Writing each row's queen column from top to bottom, the boards are 1, 3, 0, 2 and 2, 0, 3, 1: mirror images, counted as two. Every other choice puts two queens on a shared column or diagonal.
challenge icon

Follow-up

Can you count only the boards that stay different after rotating and mirroring the board? For n = 8, the 92 boards fall into 12 such groups.

Reset code
def totalNQueens(n):
    # Write code here

Test #1

Test #2

Argument

n = 4

Expected Output

2