There is a variant of the well-known N-queens problem which involves queens and knights and is said to be "considerably more difficult" 1. The problem statement is as follows:
You must place an equal number of knights ♞ and queens ♛ on a chessboard such that no piece attacks any other piece. What is the maximum number of pieces you can so place on the board, and how many different ways can you do it?
In this code-golf challenge, you will be given an input n between 3 and 32 (in a way that is the most suitable for your language). For a given n, there might be zero or more solutions to the above problem. In case there is no solution, you must output/return nothing (nil, empty string, false, ...). Otherwise, you must give two results:
- A solution board (see below) for size n where it is not possible to add a queen or a knight chess piece without having any piece being under attack. There must be an equal number of queens and knights.
- The source of a program to be run which accepts no input and gives (i) another solution (or nothing) for the same size n, in the same format, as well as (ii) another program for the next solution (and so on...).
Note that:
- The sequence of programs must never return the same board twice, must cover all possible solutions for the problem of size n and eventually has to terminate (producing no output).
- You can either return two values, return one and print the other, or print the two return values.
- However, if you print both the board and the next program, the board must not be considered to be a part of the next program (I'd recommend printing the board in comment, or use both standard output and error streams).
- The program-as-a-return-value must be a string, not a closure.
Board format
- A board is a square of size n.
- A board cell can be empty, a queen or a knight.
- You must choose distinct values for each kind of cells (i.e. you can use other symbols than Q, N when printing the board).
- If you return a non-string board, it must be an ordered collection of the n2 values of the board (e.g. matrix, vector or list in row/column-major order, ...).
If you print the board, you can either print it squared, or as a line. For example, a solution board of size 4 can be printed as follows (spaces not required; symbols at your discretion):
Q - - - - - - - - - - - - - N -
If you feel so, you can also output:
♛ · · · · · · · · · · · · · ♞ ·
... but this is sufficient:
Q-------------N-
It does not matter if you iterate through cells in a row-major or column-major order, because there are symmetrical solutions. For example, the solutions for n=4 are:
Q------N-------- Q----------N---- Q------------N-- Q-------------N- -Q----------N--- -Q------------N- -Q-------------N --Q---------N--- --Q----------N-- --Q------------N ---QN----------- ---Q----N------- ---Q---------N-- ---Q----------N- ---NQ----------- ----Q------N---- ----Q----------N N------Q-------- -------QN------- -------Q----N--- ---N----Q------- -------NQ------- --------Q------N N----------Q---- ----N------Q---- -----------QN--- -N----------Q--- --N---------Q--- -------N----Q--- -----------NQ--- N------------Q-- --N----------Q-- ---N---------Q-- N-------------Q- -N------------Q- ---N----------Q- -N-------------Q --N------------Q ----N----------Q --------N------Q
You can also look at the solutions for n=5 as matrices; the boards contains #
, q
and n
symbols, which are empty cells of different kinds (see my answer below).
I count 2836 boards for n=6, like in Sleafar's answer (I introduced a bug when reducing byte count, but it is fixed now).
Many thanks to Sleafar for finding not one but two bugs in my code.
Score
Shortest code in bytes win.
We measure the size of the first program, the one which accepts n.
1. Queens and Knights, by Roger K.W. Hui (beware! contains a solution)
-------------------------N--------Q-
is invalid because more pieces can be added:Q--------N---------------N--------Q-
). \$\endgroup\$