Given a number n, you have to place n queens on a chess board with the size n*n while the queens cannot attack each other.
You have to print all the different solutions in the following format: 0 1 1 3 2 0 3 2
Assistant
To solve the problem of placing n queens on an n x n chessboard such that no two queens attack each other, we can use a recursive backtracking algorithm. The goal is to find all possible arrangements (solutions) where each queen is placed in a unique row and unique column, and no two queens share the same diagonal.
Here's how we can approach the problem:
Representation of the Board and Queens:
We use an array queen_positions to keep track of the column positions of the queens, where the index represents the row, and the value at that index represents the column.
For example, queen_positions[i] = c means there is a queen at row i, column c.
Tracking Conflicts:
To ensure that no two queens attack each other, we need to track conflicts in columns and diagonals.
Columns: We use a boolean array columns to track which columns have queens.
Major Diagonals (\): The major diagonal of a cell (r, c) can be identified by r - c. Since this can range from -(n - 1) to n - 1, we shift the index to be in the range 0 to 2n - 2 by adding n - 1 to it.
Minor Diagonals (/): The minor diagonal of a cell (r, c) can be identified by r + c, which ranges from 0 to 2n - 2.
Recursive Backtracking Algorithm:
We define a recursive function backtrack(r) that attempts to place a queen in row r.
For each row r, we iterate through all columns c from 0 to n - 1.
Before placing a queen at (r, c), we check if the column c, major diagonal r - c + n - 1, and minor diagonal r + c are unoccupied.
If the position is safe, we place the queen and mark the column and diagonals as occupied.
We then proceed to place the queen in the next row by calling backtrack(r + 1).
If we reach r == n, it means we have successfully placed all queens, and we record the solution.
After exploring each branch, we backtrack by removing the queen and marking the positions as unoccupied to explore other possibilities.
Recording the Solutions:
Each time we find a valid arrangement, we record the positions of the queens in the required format: a sequence of numbers where each pair row column represents the position of a queen.
Since rows are assigned incrementally from 0 to n - 1 in the recursion, we only need to record the columns.
Printing the Solutions:
After finding all solutions, we print each solution on a separate line in the specified format.
Here's the complete Python code implementing the above approach:
python
def solve_n_queens(n):
solutions = []
queen_positions = []
columns = [False]*n
diag1 = [False]*(2*n-1) # r - c + n - 1
diag2 = [False]*(2*n-1) # r + c
def backtrack(r):
if r == n:
# Found a solution
solution = []
for i in range(n):
solution.append(i)
solution.append(queen_positions[i])
solutions.append(solution)
return
for c in range(n):
if not columns[c] and not diag1[r - c + n - 1] and not diag2[r + c]:
# Place the queen
columns[c] = True
diag1[r - c + n - 1] = True
diag2[r + c] = True
queen_positions.append(c)
backtrack(r + 1)
# Backtrack
columns[c] = False
diag1[r - c + n - 1] = False
diag2[r + c] = False
queen_positions.pop()
backtrack(0)
# Output all solutions
for sol in solutions:
print(' '.join(map(str, sol)))
# Example usage:
n = int(input())
solve_n_queens(n)
Example Input and Output:
If you input 4, the program will output:
code
0 1 1 3 2 0 3 2
0 2 1 0 2 3 3 1
Each line represents a different solution to the 4-queens problem, with the queen positions printed in the format row column for each queen.
Explanation of the Output:
For n = 4, there are two possible solutions to the 4-queens problem. The output lists the positions of the queens for each solution:
First Solution:
Queen at (0, 1)
Queen at (1, 3)
Queen at (2, 0)
Queen at (3, 2)
Second Solution:
Queen at (0, 2)
Queen at (1, 0)
Queen at (2, 3)
Queen at (3, 1)
Note:
The indices for rows and columns start from 0.
The program efficiently generates all possible solutions using backtracking and ensures that all unique arrangements are found.
This method can solve the n-queens problem for reasonable values of n (typically n <= 12) within a practical amount of time.