Talk:Peaceful chess queen armies: Difference between revisions

From Rosetta Code
Content added Content deleted
(/* Simplified Python exhaustive search^/)
 
(→‎Error in solution?: Maximim not asked for in task description.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Simplified Python exhaustive search==
==Original Python exhaustive search==
I was experimenting with various things when doing the Python.
I was experimenting with various things when doing the Python.
This is it simplified:
This is the original:


Exhaustive search.
<lang python>from itertools import combinations, product, count
<lang python>from itertools import combinations, count
from functools import lru_cache, reduce
from functools import lru_cache, reduce



# n-by-n board
n = 5

def _2d(n=n):
for i in range(n):
print(' '.join(f'{i},{j}' for j in range(n)))

def _1d(n=n):
for i in range(0, n*n, n):
print(', '.join(f'{i+j:2}' for j in range(n)))


_bbullet, _wbullet = '\u2022\u25E6'
_bbullet, _wbullet = '\u2022\u25E6'
#_bqueen, _wqueen = 'BW'
_bqueen, _wqueen = '\u265B\u2655'
_bqueenh, _wqueenh = '&#x265b;', '<font color="green">&#x2655;</font>'
_or = set.__or__
_or = set.__or__



def place(m, n):
def place(m, n):
"Place m black and white queens, peacefully, on an n-by-n board"
"Place m black and white queens, peacefully, on an n-by-n board"
board = set(product(range(n), repeat=2)) # (x, y) tuples
# 2-D Board as 1-D array: 2D(x, y) == 1D(t%n, t//n)
board = set(range(n*n))

#placements = list(combinations(board, m))
placements = {frozenset(c) for c in combinations(board, m)}
placements = {frozenset(c) for c in combinations(board, m)}
for blacks in placements:
for blacks in placements:
Line 18: Line 38:
(queen_attacks_from(pos, n) for pos in blacks),
(queen_attacks_from(pos, n) for pos in blacks),
set())
set())
for whites in {frozenset(c) # Never on blsck attacking squares
#for whites in placements:
for c in combinations(board - black_attacks, m)}:
for whites in {frozenset(c) for c in combinations(board - black_attacks, m)}:
if not black_attacks & whites:
if not black_attacks & whites:
return blacks, whites
return blacks, whites
Line 25: Line 45:


@lru_cache(maxsize=None)
@lru_cache(maxsize=None)
def queen_attacks_from(pos, n):
def queen_attacks_from(pos, n=n):
x0, y0 = pos
a = set([pos]) # Its position
a = set([pos]) # Its position
a.update((x, y0) for x in range(n)) # Its row
a.update(range(pos//n*n, pos//n*n+n)) # Its row
a.update((x0, y) for y in range(n)) # Its column
a.update(range(pos%n, n*n, n)) # Its column
# Diagonals
# Diagonals
x0, y0 = pos%n, pos//n
for x1 in range(n):
for x1 in range(n):
# l-to-r diag
# l-to-r diag
y1 = y0 -x0 +x1
y1 = y0 -x0 +x1
if 0 <= y1 < n:
if 0 <= y1 < n:
a.add((x1, y1))
a.add(x1 + y1 * n)
# r-to-l diag
# r-to-l diag
y1 = y0 +x0 -x1
y1 = y0 +x0 -x1
if 0 <= y1 < n:
if 0 <= y1 < n:
a.add((x1, y1))
a.add(x1 + y1 * n)
return a
return a


def pboard(black_white, n):
def pboard(black_white=None, n=n):
"Print board"
if black_white is None:
if black_white is None:
blk, wht = set(), set()
blk, wht = set(), set()
Line 50: Line 69:
print(f"## {len(blk)} black and {len(wht)} white queens "
print(f"## {len(blk)} black and {len(wht)} white queens "
f"on a {n}-by-{n} board:", end='')
f"on a {n}-by-{n} board:", end='')
for x, y in product(range(n), repeat=2):
for xy in range(n*n):
if y == 0:
if xy %n == 0:
print()
print()
xy = (x, y)
ch = ('?' if xy in blk and xy in wht
ch = ('?' if xy in blk and xy in wht
else 'B' if xy in blk
else _bqueen if xy in blk
else 'W' if xy in wht
else _wqueen if xy in wht
else _bbullet if (x + y)%2 else _wbullet)
else _bbullet if (xy%n + xy//n)%2 else _wbullet)
print('%s' % ch, end='')
print('%s' % ch, end='')
print()
print()

def hboard(black_white=None, n=n):
if black_white is None:
blk, wht = set(), set()
else:
blk, wht = black_white
out = (f"<br><b>## {len(blk)} black and {len(wht)} white queens "
f"on a {n}-by-{n} board:</b><br>\n")
out += "<table>\n "
tbl = ''
for xy in range(n*n):
if xy %n == 0:
tbl += '</tr>\n <tr>\n'
ch = ('<span style="color:red">?</span>' if xy in blk and xy in wht
else _bqueenh if xy in blk
else _wqueenh if xy in wht
else "")
bg = "" if (xy%n + xy//n)%2 else ' bgcolor="silver"'
tbl += f' <td style="width:16pt; height:16pt;"{bg}>{ch}</td>\n'
out += tbl[7:]
out += '</tr>\n</table>\n<br>\n'
return out


if __name__ == '__main__':
if __name__ == '__main__':
n=2
n=2
html = ''
for n in range(2, 7):
for n in range(2, 7):
print()
print()
queen_attacks_from.cache_clear() # memoization cache
#
for m in count(1):
for m in count(1):
ans = place(m, n)
ans = place(m, n)
if ans[0]:
if ans[0]:
pboard(ans, n)
pboard(ans, n)
html += hboard(ans, n)
else:
else:
print (f"# Can't place {m}+ queens on a {n}-by-{n} board")
comment = f"# Can't place {m}+ queens on a {n}-by-{n} board"
print (comment)
html += f"<b>{comment}</b><br><br>\n\n"
break
break
#
print('\n')
print('\n')
html += '<br>\n'
#
m, n = 5, 7
m, n = 5, 7
queen_attacks_from.cache_clear()
ans = place(m, n)
ans = place(m, n)
pboard(ans, n)</lang>
pboard(ans, n)
html += hboard(ans, n)
with open('peaceful_queen_armies.htm', 'w') as f:
f.write(html)</lang>


{{out}}
{{out}}
The console output Unicode queen characters display wider than other characters in monospace font so the alternative HTML output is shown below.
<pre># Can't place 1+ queens on a 2-by-2 board

<div style="overflow:scroll; height:250px;">
<b># Can't place 1+ queens on a 2-by-2 board</b><br><br>

<br><b>## 1 black and 1 white queens on a 3-by-3 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<b># Can't place 2+ queens on a 3-by-3 board</b><br><br>

<br><b>## 1 black and 1 white queens on a 4-by-4 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 2 black and 2 white queens on a 4-by-4 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<b># Can't place 3+ queens on a 4-by-4 board</b><br><br>

<br><b>## 1 black and 1 white queens on a 5-by-5 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 2 black and 2 white queens on a 5-by-5 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
</tr>
</table>
<br>
<br><b>## 3 black and 3 white queens on a 5-by-5 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 4 black and 4 white queens on a 5-by-5 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<b># Can't place 5+ queens on a 5-by-5 board</b><br><br>

<br><b>## 1 black and 1 white queens on a 6-by-6 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 2 black and 2 white queens on a 6-by-6 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 3 black and 3 white queens on a 6-by-6 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 4 black and 4 white queens on a 6-by-6 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<br><b>## 5 black and 5 white queens on a 6-by-6 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver">&#x265b;</td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
<b># Can't place 6+ queens on a 6-by-6 board</b><br><br>

<br>
<br><b>## 5 black and 5 white queens on a 7-by-7 board:</b><br>
<table>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;">&#x265b;</td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
</tr>
<tr>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
<td style="width:16pt; height:16pt;"></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;"><font color="green">&#x2655;</font></td>
<td style="width:16pt; height:16pt;" bgcolor="silver"></td>
</tr>
</table>
<br>
</div>

--[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 10:08, 27 March 2019 (UTC)


== Error in solution? ==
## 1 black and 1 white queens on a 3-by-3 board:
◦•◦
B◦•
◦•W
# Can't place 2+ queens on a 3-by-3 board


No solutions for {8,9},{10,14} and some other boards. For {9, 12} correctly:
## 1 black and 1 white queens on a 4-by-4 board:
◦•W•
B◦•◦
◦•◦•
•◦•◦
## 2 black and 2 white queens on a 4-by-4 board:
◦B◦•
•B•◦
◦•◦•
W◦W◦
# Can't place 3+ queens on a 4-by-4 board


## 1 black and 1 white queens on a 5-by-5 board:
12 black and 12 white queens on a 9 x 9 board:
B * x * B * x * B
◦•◦•◦
* x W x * x W x *
W◦•◦•
B * x * B * x * B
◦•◦•◦
* x W x * x W x *
•◦•◦B
B * x * B * x * B
◦•◦•◦
* x W x * x W x *
## 2 black and 2 white queens on a 5-by-5 board:
B * x * B * x * B
◦•◦•W
* x W x * x W x *
•◦B◦•
x W x W x W x W x
◦•◦•◦
•◦•B•
◦W◦•◦
## 3 black and 3 white queens on a 5-by-5 board:
◦W◦•◦
•◦•◦W
B•B•◦
B◦•◦•
◦•◦W◦
## 4 black and 4 white queens on a 5-by-5 board:
◦•B•B
W◦•◦•
◦W◦W◦
W◦•◦•
◦•B•B
# Can't place 5+ queens on a 5-by-5 board


I checked C and C++ codes and compare results from https://oeis.org/A250000
## 1 black and 1 white queens on a 6-by-6 board:
◦•◦•◦•
W◦•◦•◦
◦•◦•◦•
•◦•◦B◦
◦•◦•◦•
•◦•◦•◦
## 2 black and 2 white queens on a 6-by-6 board:
◦•◦•◦•
•◦B◦•◦
◦•◦•◦•
•◦•B•◦
◦•◦•◦•
W◦•◦W◦
## 3 black and 3 white queens on a 6-by-6 board:
◦•B•◦•
•B•◦•◦
◦•◦W◦W
•◦•◦•◦
W•◦•◦•
•◦•◦B◦
## 4 black and 4 white queens on a 6-by-6 board:
WW◦•W•
•W•◦•◦
◦•◦•◦B
•◦B◦•◦
◦•◦B◦•
•◦•B•◦
## 5 black and 5 white queens on a 6-by-6 board:
◦•W•W•
B◦•◦•◦
◦•W•◦W
B◦•◦•◦
◦•◦•◦W
BB•B•◦
# Can't place 6+ queens on a 6-by-6 board


Hi, Please sign your contribution bove, thanks. --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 10:58, 19 January 2020 (UTC)


:If you carefully check the wording of the task, it doesn't ask for ''maximum'' numbers of armies.
## 5 black and 5 white queens on a 7-by-7 board:
:Language implementations might well have implied this though which may need individual mods.
◦•◦•B•◦
:Thanks for spotting it. --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 10:58, 19 January 2020 (UTC)
•W•◦•◦W
◦•◦•B•◦
B◦•◦•◦•
◦•B•◦•◦
•◦•B•◦•
◦W◦•◦WW</pre>

Latest revision as of 10:59, 19 January 2020

Original Python exhaustive search

I was experimenting with various things when doing the Python. This is the original:

Exhaustive search. <lang python>from itertools import combinations, count from functools import lru_cache, reduce


  1. n-by-n board

n = 5

def _2d(n=n):

 for i in range(n):
     print('  '.join(f'{i},{j}' for j in range(n)))

def _1d(n=n):

 for i in range(0, n*n, n):
     print(',  '.join(f'{i+j:2}' for j in range(n)))

_bbullet, _wbullet = '\u2022\u25E6'

  1. _bqueen, _wqueen = 'BW'

_bqueen, _wqueen = '\u265B\u2655' _bqueenh, _wqueenh = '♛', '' _or = set.__or__


def place(m, n):

   "Place m black and white queens, peacefully, on an n-by-n board"
   
   # 2-D Board as 1-D array:  2D(x, y) == 1D(t%n, t//n)
   board = set(range(n*n))
   #placements = list(combinations(board, m))
   placements = {frozenset(c) for c in combinations(board, m)}
   for blacks in placements:
       black_attacks = reduce(_or, 
                              (queen_attacks_from(pos, n) for pos in blacks), 
                              set())
       #for whites in placements:
       for whites in {frozenset(c) for c in combinations(board - black_attacks, m)}:
           if not black_attacks & whites:
               return blacks, whites
   return set(), set()

@lru_cache(maxsize=None) def queen_attacks_from(pos, n=n):

   a = set([pos])    # Its position
   a.update(range(pos//n*n, pos//n*n+n))    # Its row
   a.update(range(pos%n, n*n, n))           # Its column
   # Diagonals
   x0, y0 = pos%n, pos//n
   for x1 in range(n):
       # l-to-r diag
       y1 = y0 -x0 +x1
       if 0 <= y1 < n: 
           a.add(x1 + y1 * n)
       # r-to-l diag
       y1 = y0 +x0 -x1
       if 0 <= y1 < n: 
           a.add(x1 + y1 * n)
   return a

def pboard(black_white=None, n=n):

   if black_white is None: 
       blk, wht = set(), set()
   else:
       blk, wht = black_white
   print(f"## {len(blk)} black and {len(wht)} white queens "
         f"on a {n}-by-{n} board:", end=)
   for xy in range(n*n):
       if xy %n == 0:
           print()
       ch = ('?' if xy in blk and xy in wht 
             else _bqueen if xy in blk
             else _wqueen if xy in wht
             else _bbullet if (xy%n + xy//n)%2 else _wbullet)
       print('%s' % ch, end=)
   print()

def hboard(black_white=None, n=n):

   if black_white is None: 
       blk, wht = set(), set()
   else:
       blk, wht = black_white
   out = (f"
## {len(blk)} black and {len(wht)} white queens " f"on a {n}-by-{n} board:
\n")

out += "

\n " tbl = for xy in range(n*n): if xy %n == 0: tbl += '\n \n' ch = ('?' if xy in blk and xy in wht else _bqueenh if xy in blk else _wqueenh if xy in wht else "") bg = "" if (xy%n + xy//n)%2 else ' bgcolor="silver"' tbl += f' \n'
   out += tbl[7:]
out += '\n
{ch}

\n
\n'

   return out

if __name__ == '__main__':

   n=2
   html = 
   for n in range(2, 7):
       print()
       queen_attacks_from.cache_clear()    # memoization cache
       #
       for m in count(1):
           ans = place(m, n)
           if ans[0]:
               pboard(ans, n)
               html += hboard(ans, n)
           else:
               comment = f"# Can't place {m}+ queens on a {n}-by-{n} board"
               print (comment)
               html += f"{comment}

\n\n" break print('\n') html += '
\n' # m, n = 5, 7 queen_attacks_from.cache_clear() ans = place(m, n) pboard(ans, n) html += hboard(ans, n) with open('peaceful_queen_armies.htm', 'w') as f: f.write(html)</lang>
Output:

The console output Unicode queen characters display wider than other characters in monospace font so the alternative HTML output is shown below.

# Can't place 1+ queens on a 2-by-2 board


## 1 black and 1 white queens on a 3-by-3 board:


# Can't place 2+ queens on a 3-by-3 board


## 1 black and 1 white queens on a 4-by-4 board:



## 2 black and 2 white queens on a 4-by-4 board:


# Can't place 3+ queens on a 4-by-4 board


## 1 black and 1 white queens on a 5-by-5 board:



## 2 black and 2 white queens on a 5-by-5 board:



## 3 black and 3 white queens on a 5-by-5 board:



## 4 black and 4 white queens on a 5-by-5 board:


# Can't place 5+ queens on a 5-by-5 board


## 1 black and 1 white queens on a 6-by-6 board:



## 2 black and 2 white queens on a 6-by-6 board:



## 3 black and 3 white queens on a 6-by-6 board:



## 4 black and 4 white queens on a 6-by-6 board:



## 5 black and 5 white queens on a 6-by-6 board:


# Can't place 6+ queens on a 6-by-6 board



## 5 black and 5 white queens on a 7-by-7 board:


--Paddy3118 (talk) 10:08, 27 March 2019 (UTC)

Error in solution?

No solutions for {8,9},{10,14} and some other boards. For {9, 12} correctly:

12 black and 12 white queens on a 9 x 9 board:
B * x * B * x * B
* x W x * x W x *
B * x * B * x * B
* x W x * x W x *
B * x * B * x * B
* x W x * x W x *
B * x * B * x * B
* x W x * x W x *
x W x W x W x W x

I checked C and C++ codes and compare results from https://oeis.org/A250000

Hi, Please sign your contribution bove, thanks. --Paddy3118 (talk) 10:58, 19 January 2020 (UTC)

If you carefully check the wording of the task, it doesn't ask for maximum numbers of armies.
Language implementations might well have implied this though which may need individual mods.
Thanks for spotting it. --Paddy3118 (talk) 10:58, 19 January 2020 (UTC)