-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
149 lines (123 loc) · 5.22 KB
/
board.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import numpy as np
from collections import defaultdict
def choose2(n):
return (n * (n - 1)) // 2
class Queen:
def __init__(self, x, y):
self.x = x
self.move(y)
def move(self, y):
self.y = y
self.incraesing_diag = self.x + self.y
self.decreasing_diag = self.x - self.y
def get_diagonals(self):
return self.incraesing_diag, self.decreasing_diag
def __repr__(self):
return f"Queen({self.x}, {self.y})"
def __iter__(self):
return iter([self.x, self.y])
class Board:
def __init__(self, N):
self.N = N
self.queens = []
self.nb_conflicts = 0
def count_conflicts(self):
conflicts = 0
self.increasing_diag_counter = defaultdict(int)
self.decreasing_diag_counter = defaultdict(int)
for i in range(self.N):
increasing_diag, decreasing_diag = self.queens[i].get_diagonals()
self.increasing_diag_counter[increasing_diag] += 1
self.decreasing_diag_counter[decreasing_diag] += 1
# There are n_diag choose 2 conflicts over the diagonal
for n_diag in self.increasing_diag_counter.values():
conflicts += choose2(n_diag)
for n_diag in self.decreasing_diag_counter.values():
conflicts += choose2(n_diag)
self.nb_conflicts = conflicts
def init_board(self):
P = np.random.permutation(self.N)
for x, y in enumerate(P):
self.queens.append(Queen(x, y))
self.count_conflicts()
def print_board(self):
T = np.zeros((self.N, self.N), dtype=int)
for queen in self.queens:
T[queen.x][queen.y] += 1
print("╔═", end="")
for _ in range(self.N - 1):
print("╤═", end="")
print("╗")
for i in range(self.N):
print(f"║{ ' ' if T[i][0] == 0 else 'x'}", end="")
for j in range(1, self.N):
print(f"│{ ' ' if T[i][j] == 0 else 'x'}", end="")
print("║")
if i != self.N - 1:
print("╟─", end="")
for _ in range(self.N - 1):
print("┼─", end="")
print("╢")
print("╚═", end="")
for _ in range(self.N - 1):
print("╧═", end="")
print("╝")
self.count_conflicts()
print(f"Number of conflicts {self.nb_conflicts}")
def var_move(self, i, j):
if i == j:
return 0, self.increasing_diag_counter, self.decreasing_diag_counter
new_queen1 = Queen(self.queens[i].x, self.queens[j].y)
new_queen2 = Queen(self.queens[j].x, self.queens[i].y)
var = 0
tmp_increasing_counter, tmp_decreasing_counter = (
self.increasing_diag_counter.copy(),
self.decreasing_diag_counter.copy(),
)
conflicts = int(self.nb_conflicts)
increasing_diags_to_update = set()
decreasing_diags_to_update = set()
# Remove old conflicts
old_increasing_diag1, old_decreasing_diag1 = self.queens[i].get_diagonals()
old_increasing_diag2, old_decreasing_diag2 = self.queens[j].get_diagonals()
tmp_increasing_counter[old_increasing_diag1] -= 1
tmp_decreasing_counter[old_decreasing_diag1] -= 1
tmp_increasing_counter[old_increasing_diag2] -= 1
tmp_decreasing_counter[old_decreasing_diag2] -= 1
increasing_diags_to_update.add(old_increasing_diag1)
increasing_diags_to_update.add(old_increasing_diag2)
decreasing_diags_to_update.add(old_decreasing_diag1)
decreasing_diags_to_update.add(old_decreasing_diag2)
# Add new conflicts
new_increasing_diag1, new_decreasing_diag1 = new_queen1.get_diagonals()
new_increasing_diag2, new_decreasing_diag2 = new_queen2.get_diagonals()
tmp_increasing_counter[new_increasing_diag1] += 1
tmp_decreasing_counter[new_decreasing_diag1] += 1
tmp_increasing_counter[new_increasing_diag2] += 1
tmp_decreasing_counter[new_decreasing_diag2] += 1
increasing_diags_to_update.add(new_increasing_diag1)
increasing_diags_to_update.add(new_increasing_diag2)
decreasing_diags_to_update.add(new_decreasing_diag1)
decreasing_diags_to_update.add(new_decreasing_diag2)
for diag in increasing_diags_to_update:
conflicts -= choose2(self.increasing_diag_counter[diag]) - choose2(
tmp_increasing_counter[diag]
)
for diag in decreasing_diags_to_update:
conflicts -= choose2(self.decreasing_diag_counter[diag]) - choose2(
tmp_decreasing_counter[diag]
)
var = conflicts - self.nb_conflicts
return var, tmp_increasing_counter, tmp_decreasing_counter
def move_queen(self, i, j):
if i == j:
return
# update number of conflict
var, tmp_increasing_counter, tmp_decreasing_counter = self.var_move(i, j)
self.nb_conflicts += var
self.increasing_diag_counter = tmp_increasing_counter
self.decreasing_diag_counter = tmp_decreasing_counter
# update board
y1, y2 = self.queens[i].y, self.queens[j].y
self.queens[i].move(y2)
self.queens[j].move(y1)