FunctionRacerProblemsRace

Queens Around Obstacles

HardRecursionPython, JavaScript, TypeScript, Java, C++

Count the ways to place n queens on an n by n chessboard so that no two queens share a row, a column or a diagonal, with one extra rule: the cells listed in blocked, given as [row, col] pairs with 0-based indices, cannot hold a queen. Return the number of valid placements. Two placements are different when some queen stands on a different cell.

For example, with n = 4 and no blocked cells there are 2 placements; blocking the cell [0, 1] rules out one of them, leaving 1.

Example 1

n 4
blocked []
returns 2

The two classic 4-queen placements put the first-row queen in column 1 or in column 2.

Example 2

n 4
blocked [[0,1]]
returns 1

Blocking row 0, column 1 removes the placement that uses that cell, so one placement remains.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems