-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn-queen.py
More file actions
47 lines (40 loc) · 1.47 KB
/
n-queen.py
File metadata and controls
47 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def is_safe(board, row, col, N):
# Check the same column
for i in range(row):
if board[i][col] == 1:
return False
# Check the upper-left diagonal
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 1:
return False
# Check the upper-right diagonal
for i, j in zip(range(row, -1, -1), range(col, N, 1)):
if board[i][j] == 1:
return False
return True
def solve_n_queens_util(board, row, N, solutions):
# If all queens are placed successfully, store the solution
if row >= N:
solution = []
for i in range(N):
solution.append(''.join(' Q ' if board[i][j] else ' . ' for j in range(N)))
solutions.append(solution)
return
# Try placing a queen in each column of the current row
for col in range(N):
if is_safe(board, row, col, N):
board[row][col] = 1 # Place the queen
solve_n_queens_util(board, row + 1, N, solutions)
board[row][col] = 0 # Backtrack
def solve_n_queens(N):
board = [[0 for _ in range(N)] for _ in range(N)] # Create an N x N chessboard
solutions = [] # Store all solutions
solve_n_queens_util(board, 0, N, solutions)
return solutions
N = 4
solutions = solve_n_queens(N)
for idx, solution in enumerate(solutions):
print(f"Solution {idx + 1}:")
for row in solution:
print(row)
print()