Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

N-Queens Problem

XPChallenge Points: 30
levelLevel: Hard

The N-Queens problem asks you to place n queens on an n x n chessboard so that no two queens attack each other — meaning no two queens share the same row, column, or diagonal.

Your task is to return all distinct solutions to this puzzle.
Each solution should be represented as an array of strings, where:

  • 'Q' represents a queen

  • '.' represents an empty space

You may return the solutions in any order.

Example 1:

Input: n = 4

Output: [[".Q..","...Q","Q...","..Q."], ["..Q.","Q...","...Q",".Q.."]]

Explanation:

There exist two distinct arrangements for placing 4 queens.

Example 2:

Input: n = 1

Output: [["Q"]]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result