📢 This article was translated by gemini-3-flash-preview
Problem Description
The n-Queens problem involves placing $n$ queens on an $n \times n$ chessboard such that no two queens attack each other. This means no two queens can be in the same row, same column, or on the same diagonal (following standard chess queen movement rules).
Problem Analysis
We can solve the n-Queens problem using the following approach:
- Place the $i$-th queen in the $i$-th row.
- Starting from the first queen, try each column in its corresponding row (the $i$-th queen corresponds to the $i$-th row) starting from the first column.
- If a position is valid (no attacks), place the queen and move to the next queen.
- If it conflicts with existing queens, try the next column.
- If all columns in the current row are exhausted, backtrack to the previous queen and move it to its next possible column.
Repeat this process until all possible configurations are found.
C Code
| |