N Queens

Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.

Given an integern, return all distinct solutions to then-queens puzzle.

Each solution contains a distinct board configuration of then-queens' placement, where'Q'and'.'both indicate a queen and an empty space respectively.

For example, There exist two distinct solutions to the 4-queens puzzle:

Approach 1: Brute Force

Check every permutable position on the chess board. This can be done by mapping each position on the board to a unique key that identifies it's i,j position - and then checking all n(n)Pn_{n(n)}P_n arrangement's, where n is the number of queens.

Example Output

Last updated

Was this helpful?