N-Queens
Practice
3.9 (126 votes)
Backtracking
Easy
Problem
87% Success 18108 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
Given a chess board having \(N \times N\) cells, you need to place N queens on the board in such a way that no queen attacks any other queen.
Input:
The only line of input consists of a single integer denoting N.
Output:
If it is possible to place all the N queens in such a way that no queen attacks another queen, then print N lines having N integers. The integer in \(i^{th}\) line and \(j^{th}\) column will denote the cell \((i,j)\) of the board and should be 1 if a queen is placed at \((i,j)\) otherwise 0. If there are more than way of placing queens print any of them. If it is not possible to place all N queens in the desired way, then print "Not possible" (without quotes).
Constraints:
\(1 \le N \le 10\).
Submissions
Please login to view your submissions
Similar Problems
Points:20
12 votes
Tags:
RecursionRecursion and BacktrackingBasic Programming
Points:30
9 votes
Tags:
Depth First SearchEasyGraphsImplementationRecursion
Points:20
92 votes
Tags:
BacktrackingEasyMathRecursion
Editorial