N-Queens Count

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write totalNQueens(n): return how many ways there are to place n chess queens on an n by n board so that no two attack each other, meaning no two share a row, a column or a diagonal. n is a whole number from 1 to 11. Placements that are rotations or reflections of each other count as different. For example, totalNQueens(4) is 2. The checks include n = 10, which has 724 ways, and n = 11, which has 2,680, so trying every arrangement of queens and checking it at the end is too slow: place one queen per row, and back up as soon as a square is attacked.

Your code must pass

  • Counts the ways for small boards
  • Returns 0 when no placement works
  • Counts 92 ways on the classic 8 by 8 board
  • Is fast enough for n = 10 and n = 11