Skip to content

Commit dc852ec

Browse files
authored
Minor correction to nqueens.py
Preparing to use this for a demo, I noticed a minor inconsistency. The `axis` kwarg specifies the axis over which to sum, with 0 being rows and 1 being columns. But to get row totals you have to sum over columns, and vice versa. Although the symmetry in this problem means it makes no difference, I thought it'd be better to avoid confusion.
1 parent 54b3012 commit dc852ec

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

examples/nqueens.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
x = h.addBinaries(N, N)
1515

16-
h.addConstrs(x.sum(axis=0) == 1) # each row has exactly one queen
17-
h.addConstrs(x.sum(axis=1) == 1) # each col has exactly one queen
16+
h.addConstrs(x.sum(axis=1) == 1) # each row has exactly one queen
17+
h.addConstrs(x.sum(axis=0) == 1) # each col has exactly one queen
1818

1919
y = np.fliplr(x)
2020
h.addConstrs(x.diagonal(k).sum() <= 1 for k in range(-N + 1, N)) # each diagonal has at most one queen
@@ -26,4 +26,4 @@
2626
print('Queens:')
2727

2828
for i in range(N):
29-
print(''.join('Q' if sol[i, j] > 0.5 else '*' for j in range(N)))
29+
print(''.join('Q' if sol[i, j] > 0.5 else '*' for j in range(N)))

0 commit comments

Comments
 (0)