Skip to main content

Hard backtracking · recursion · grids

Quiet queens

Eight queens on one chessboard, and not one of them allowed to see another.

Write countQueens(n): count the ways to place n queens on an n by n board so that no two share a row, a column or a diagonal. n is at least 1.

Keep a board of 0s and 1s (1 is a queen) and call show(board) each time you complete a full placement. The stage draws every solution you find.

Example: countQueens(4) returns 2.

Tests you can see

  • countQueens(4)2
  • countQueens(1)1
  • countQueens(5)10

More tests are hidden. The server runs them all before a time counts.

Call show() while your code works and the run plays back here.

javascriptcountQueens()

Tab indents · Cmd/Ctrl+Enter runs