forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqueen.py
40 lines (30 loc) · 879 Bytes
/
nqueen.py
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
def solve_n_queens(n):
def is_safe(board, row, col):
for i in range(row):
if board[i] == col or \
board[i] - i == col - row or \
board[i] + i == col + row:
return False
return True
def solve_nq_util(board, row):
if row >= n:
result.append(board[:])
return
for col in range(n):
if is_safe(board, row, col):
board[row] = col
solve_nq_util(board, row + 1)
board[row] = -1
result = []
board = [-1] * n
solve_nq_util(board, 0)
return result
n = 4
solutions = solve_n_queens(n)
for solution in solutions:
print("Solution:")
for i in range(n):
row = ['.'] * n
row[solution[i]] = 'Q'
print(" ".join(row))
print()