Eight Queens
The Problem
Place eight queens on a chessboard so that none is captured by any other.
A placement is represented as a permutation of !8:
p:2 5 3 0 7 4 6 1
p[0] says that a queen is placed in column 2 of row 0.
p[1] says that a queen is placed in column 5 of row 1.
:
A permutation is displayed with the 'board' function 'bd':
bd:`0:"_Q"(!8)=/:p
bd
Q.......
....Q...
.......Q
.....Q..
..Q.....
......Q.
.Q......
...Q....
Horizontal and vertical captures are not possible on a board represented by a permutation.
The Algorithm
place a queen in column j of row 0
in row 1, mark:
vertical column j
left diagonal column j-1
right diagonal column j+1
place a queen in an unmarked column k of row 1
in row 2, mark:
vertical columns j, k
left diagonal columns j-2, k-1
right diagonal colunms j+2, k+1
&c.