-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.py
More file actions
45 lines (36 loc) · 1.25 KB
/
main2.py
File metadata and controls
45 lines (36 loc) · 1.25 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
from typing import List
class Board:
def __init__(self, size: int):
self.N = size
self.queens: List[int] = []
self.matches: List[List[int]] = []
def is_queen_valid(self, row: int, col: int):
for r, c in enumerate(self.queens):
if r == row or c == col or abs(row - r) == abs(col - c):
return False
return True
def solution(self) -> List[List[int]]:
self.queens = []
col = row = 0
i = 0
while True:
while col < self.N and not self.is_queen_valid(row, col):
col += 1
if col < self.N:
self.queens.append(col)
if row + 1 >= self.N:
self.matches.append(self.queens.copy())
self.queens.pop()
col = self.N
else:
row += 1
col = 0
if col >= self.N:
# not possible to place a queen in this row anymore
if row == 0:
return self.matches.copy() # all combinations were tried
col = self.queens.pop() + 1 # Increment column
row -= 1
i += 1
board = Board(4)
print(board.solution())