N-Queens
Practice this coding question on Coding Gurukul.
Problem statement
A chess exhibition needs layouts in which every queen has an unobstructed safety zone. The organizer wants all possible layouts for a square board. Your task Place n queens on an n by n board so that no two share a row, column or diagonal, and enumerate every distinct placement. Important details Represent a board by n column numbers: the ith number is the 1-based column occupied in row i. Print these integer sequences in numeric lexicographic order. Rotations and reflections count as different layouts when their column sequences differ. If no placement exists, print only 0. Worked example For n = 4, the two column sequences are 2 4 1 3 and 3 1 4 2. Each uses every row and column once, and neither has a diagonal conflict.
Input format
One integer n.
Output format
Print the number of layouts, then one column sequence per line in numeric lexicographic order.
Constraints
1 <= n <= 9.
Sample input
4
Sample output
2 2 4 1 3 3 1 4 2
Sign in to run or submit a solution in C++, Java or Python.