Files
rose-ash/lib/apl/tests/programs/n-queens.apl
giles e37167a58e
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m5s
apl: n-queens via permute + diagonal filter, q(8)=92 (+10 tests, 306/306)
2026-05-07 05:46:54 +00:00

19 lines
847 B
APL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
⍝ N-Queens — count solutions to placing N non-attacking queens on N×N
⍝ A solution is encoded as a permutation P of 1..N where P[i] is the
⍝ column of the queen in row i. Rows and columns are then automatically
⍝ unique (it's a permutation). We must additionally rule out queens
⍝ sharing a diagonal: |i-j| = |P[i]-P[j]| for any pair.
⍝ Backtracking via reduce — the classic Roger Hui style:
⍝ queens ← {≢{⍵,¨⍨↓(0=∊(¨⍳⍴⍵)≠.+|⍵)/⍳⍴⍵}/(⍳⍵)⍴⊂⍳⍵}
⍝ Plain reading:
⍝ permute 1..N, keep those where no two queens share a diagonal.
⍝ Known solution counts (OEIS A000170):
⍝ N 1 2 3 4 5 6 7 8 9 10
⍝ q(N) 1 0 0 2 10 4 40 92 352 724
queens {({(i j) (|i-j)|(P[i])-(P[j])}permutations )}