Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check whether a board is valid N queens solution or not in python
The N-Queens puzzle is a classic problem where we need to place N queens on an N×N chessboard such that no two queens attack each other. In this article, we'll learn how to verify whether a given board configuration is a valid N-Queens solution.
A valid N-Queens solution must satisfy these conditions:
- Exactly N queens are placed on the board
- No two queens share the same row, column, or diagonal
Understanding the Problem
Given an N×N matrix with 1s representing queens and 0s representing empty cells, we need to check if it's a valid N-Queens solution. Two queens attack each other if they are on the same row, column, or diagonal.
Algorithm Approach
We use four sets to track queen positions and detect conflicts:
- rows: Tracks which rows have queens
- cols: Tracks which columns have queens
- diags: Tracks main diagonals using (i - j)
- rev_diags: Tracks anti-diagonals using (i + j)
Implementation
def is_valid_n_queens(matrix):
n = len(matrix)
rows = set()
cols = set()
diags = set()
rev_diags = set()
for i in range(n):
for j in range(n):
if matrix[i][j] == 1:
rows.add(i)
cols.add(j)
diags.add(i - j)
rev_diags.add(i + j)
return len(rows) == len(cols) == len(diags) == len(rev_diags) == n
# Test with a valid 5-Queens solution
matrix = [
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[1, 0, 0, 0, 0]
]
result = is_valid_n_queens(matrix)
print(f"Is valid N-Queens solution: {result}")
Is valid N-Queens solution: True
How the Algorithm Works
For each queen position (i, j) in the matrix:
- Row check: Add row index i to rows set
- Column check: Add column index j to cols set
- Main diagonal: Add (i - j) to diags set
- Anti-diagonal: Add (i + j) to rev_diags set
If all sets have exactly N elements, no conflicts exist.
Testing Invalid Cases
# Test with invalid solution (two queens on same row)
invalid_matrix = [
[1, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
[0, 0, 0, 0]
]
result = is_valid_n_queens(invalid_matrix)
print(f"Invalid solution result: {result}")
# Test with too few queens
incomplete_matrix = [
[0, 1, 0, 0],
[0, 0, 0, 1],
[1, 0, 0, 0],
[0, 0, 0, 0]
]
result = is_valid_n_queens(incomplete_matrix)
print(f"Incomplete solution result: {result}")
Invalid solution result: False Incomplete solution result: False
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(N²) | Iterate through entire N×N matrix |
| Space | O(N) | Four sets, each storing at most N elements |
Conclusion
This algorithm efficiently validates N-Queens solutions by using sets to track row, column, and diagonal conflicts. The approach has O(N²) time complexity and correctly identifies both valid and invalid board configurations.
---