← DiffPush

N queens

DiffPush Tier Recursion · Try Out All Combos O(n!) upper bound — column-by-column placement with diagonal pruning · O(n^2) to store each board plus O(n) recursion depth

Scheduling Patrols Across a Compound Grid

A security grid places n patrol drones so that no two share a column, a row, or any diagonal — one drone per file, and no clear line of sight along a slope. The dispatcher fills the grid one column at a time, trying each row in the current column, and keeps three occupancy ledgers: rows taken, diagonals sloping one way, and diagonals sloping the other. When all n columns hold a drone, the layout is archived.

Input: An integer n, the grid size and drone count.

Output: Every distinct layout as n row-strings, each 'Q' marking a drone and '.' an empty cell.

Constraints

Examples

Example 1

Input: {"n":4}
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
The 4-grid has exactly two distinct layouts, mirror images of each other.

Example 2

Input: {"n":1}
Output: [["Q"]]
A single drone is trivially safe.

Solve this in your browser →

Also on LeetCode ↗