lpetrich
Contributor
For a n*n chessboard, find how many ways one can place n chess queens on them so that none of them attack each other.
I once saw that problem used as a benchmark, and I've written versions of it in a variety of programming languages. The algorithm that I used is depth-first. It stars with looping over placing a queen on each square of it, and for each placement , it does that for the next rank toward the other end. It checks the second queen for legitimate placement, then continues to the third rank and the third queen. The queen placement stops when one has reached the other end of the board. I then do statistics on the resulting position.
On my computer, a 2.3-GHz Intel Core i5 iMac, I've found that my C++ version takes about a minute and a half, finding 14772512 valid solutions.
Here is up to 27 queens: 1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712, 365596, 2279184, 14772512, 95815104, 666090624, 4968057848, 39029188884, 314666222712, 2691008701644, 24233937684440, 227514171973736, 2207893435808352, 22317699616364044, 234907967154122528
A 1*1 board has one queen as a valid solution, and I will ignore that case in my discussion of solutions' symmetries.
Some solutions have symmetries, and the possible ones are twofold and fourfold. Twofold symmetry is symmetry under rotating by 180d, and fourfold symmetry is symmetry under rotating by 90d, 180d, or 270d. No solutions have reflection symmetry, as can easily be shown.
Some solutions have twofold symmetry, looking the same under rotation by 180d and some fourfold symmetry, but it's easy to prove that no solution can have reflection symmetry. For some queen, the corresponding reflected queen is offset from the original by a rank, a file, or a diagonal, and the two queens are thus mutually attacking.
Each fourfold solution has a related solution, a mirror-image one. Each twofold solution has three related ones, rotation by 90d, reflection in an axial (rank, file) direction, and reflection in a diagonal direction. Each no-symmetry solution has seven related ones, rotation by 90d, 180d, 270d, and reflection along each axis and each diagonal.
No symmetry, distinct: 0, 0, 0, 0, 1, 0, 4, 11, 42, 89, 329, 1765, 9197, 45647, 284743, 1846189, 11975869, 83259065, 621001708, 4878630533, 39333230881, 336375931369, 3029241762900, 28439270037332, 275986675209470, 2789712437580722, 29363495854214938
Twofold symmetry, distinct: 0, 0, 0, 0, 0, 1, 2, 1, 4, 3, 12, 18, 32, 105, 310, 734, 2006, 4526, 11046, 36035, 93740, 312673, 895310, 2917938, 8532332, 28929567, 80100756, 312717136, 900007828, 3647359506, 10816583674, 45313494882
Fourfold symmetry: distinct: 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 32, 64, 0, 0, 240, 352, 0, 0, 1664, 1632, 0, 0, 16448, 21888, 0, 0, 203392, 333952, 0, 0, 2922752, 4325376, 0, 0, 38592000, 50746368, 0, 0, 630794240, 897616896, 0, 0, 10758713344, 17514086400, 0, 0, 203437559808, 326022221824, 0, 0, 4306790547456, 6265275064320, 0, 0, 97204813266944, 145913049251840, 0, 0
For fourfold symmetry, there are solutions for sizes 4k and 4k+1, but not for sizes 4k+2 or 4k+3.
For twofold and fourfold symmetry and odd size, then one queen is in the center.
Four twofold symmetry, the rotations relate two queens, except for a queen in the center. Likewise, for fourfold symmetry, the rotations related four queens, except for a queen in the center. That accounts for what sizes are possible.
I once saw that problem used as a benchmark, and I've written versions of it in a variety of programming languages. The algorithm that I used is depth-first. It stars with looping over placing a queen on each square of it, and for each placement , it does that for the next rank toward the other end. It checks the second queen for legitimate placement, then continues to the third rank and the third queen. The queen placement stops when one has reached the other end of the board. I then do statistics on the resulting position.
On my computer, a 2.3-GHz Intel Core i5 iMac, I've found that my C++ version takes about a minute and a half, finding 14772512 valid solutions.
Here is up to 27 queens: 1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712, 365596, 2279184, 14772512, 95815104, 666090624, 4968057848, 39029188884, 314666222712, 2691008701644, 24233937684440, 227514171973736, 2207893435808352, 22317699616364044, 234907967154122528
A 1*1 board has one queen as a valid solution, and I will ignore that case in my discussion of solutions' symmetries.
Some solutions have symmetries, and the possible ones are twofold and fourfold. Twofold symmetry is symmetry under rotating by 180d, and fourfold symmetry is symmetry under rotating by 90d, 180d, or 270d. No solutions have reflection symmetry, as can easily be shown.
Some solutions have twofold symmetry, looking the same under rotation by 180d and some fourfold symmetry, but it's easy to prove that no solution can have reflection symmetry. For some queen, the corresponding reflected queen is offset from the original by a rank, a file, or a diagonal, and the two queens are thus mutually attacking.
Each fourfold solution has a related solution, a mirror-image one. Each twofold solution has three related ones, rotation by 90d, reflection in an axial (rank, file) direction, and reflection in a diagonal direction. Each no-symmetry solution has seven related ones, rotation by 90d, 180d, 270d, and reflection along each axis and each diagonal.
No symmetry, distinct: 0, 0, 0, 0, 1, 0, 4, 11, 42, 89, 329, 1765, 9197, 45647, 284743, 1846189, 11975869, 83259065, 621001708, 4878630533, 39333230881, 336375931369, 3029241762900, 28439270037332, 275986675209470, 2789712437580722, 29363495854214938
Twofold symmetry, distinct: 0, 0, 0, 0, 0, 1, 2, 1, 4, 3, 12, 18, 32, 105, 310, 734, 2006, 4526, 11046, 36035, 93740, 312673, 895310, 2917938, 8532332, 28929567, 80100756, 312717136, 900007828, 3647359506, 10816583674, 45313494882
Fourfold symmetry: distinct: 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 32, 64, 0, 0, 240, 352, 0, 0, 1664, 1632, 0, 0, 16448, 21888, 0, 0, 203392, 333952, 0, 0, 2922752, 4325376, 0, 0, 38592000, 50746368, 0, 0, 630794240, 897616896, 0, 0, 10758713344, 17514086400, 0, 0, 203437559808, 326022221824, 0, 0, 4306790547456, 6265275064320, 0, 0, 97204813266944, 145913049251840, 0, 0
For fourfold symmetry, there are solutions for sizes 4k and 4k+1, but not for sizes 4k+2 or 4k+3.
For twofold and fourfold symmetry and odd size, then one queen is in the center.
Four twofold symmetry, the rotations relate two queens, except for a queen in the center. Likewise, for fourfold symmetry, the rotations related four queens, except for a queen in the center. That accounts for what sizes are possible.