Skip to the code
cf.
+50 XP

#307 Queen Puzzle With Ad Slots

hard

A puzzle page shows an n × n chess board. The player must place n queens so that no two share a row, a column, or a diagonal. Some squares are covered by ads and can't hold a queen; they are listed in blocked as [row, col] pairs.

Write countQueenLayouts(n, blocked) that returns how many valid layouts exist. Blocked squares don't block attacks; they only can't hold a queen.

Example 1

Input: n = 4, blocked = []
Output: 2

A 4 × 4 board has two layouts: queens in columns 1, 3, 0, 2 or 2, 0, 3, 1, row by row.

Example 2

Input: n = 4, blocked = [[0,1]]
Output: 1

The ad at row 0, column 1 removes the first layout and leaves the other.

Constraints

  • 1 <= n <= 9
  • 0 <= blocked.length <= n * n
  • Each blocked square is on the board and listed once.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).