-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8 queen 2.py
62 lines (49 loc) · 1.76 KB
/
8 queen 2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class NQueens:
def __init__(self):
self.N = 8
self.min_solution_count = float('inf')
self.solution_count = 0
def print_solution(self, board):
self.solution_count += 1
print("Solution", self.solution_count, ":")
for i in range(self.N):
for j in range(self.N):
if board[i][j] == 1:
print(" Q ", end="")
else:
print(" . ", end="")
print()
print()
def is_safe(self, board, row, col):
for i in range(col):
if board[row][i] == 1:
return False
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 1:
return False
for i, j in zip(range(row, self.N), range(col, -1, -1)):
if board[i][j] == 1:
return False
return True
def solve_nq_util(self, board, col):
if col >= self.N:
self.print_solution(board)
self.min_solution_count = min(self.min_solution_count, self.solution_count)
return True
res = False
for i in range(self.N):
if self.is_safe(board, i, col):
board[i][col] = 1
res = self.solve_nq_util(board, col + 1) or res
board[i][col] = 0
return res
def solve_nq(self):
board = [[0] * self.N for _ in range(self.N)]
if not self.solve_nq_util(board, 0):
print("Solution does not exist")
return False
print("Minimum number of solutions:", self.min_solution_count)
return True
if __name__ == "__main__":
queen = NQueens()
queen.solve_nq()