4 ms·
Another: I solved the 8-queens problem for a magazine contest using a BASIC program (!) Instead of modelling the board with an 8X8 array, I used the digits 1-8
by JoeAltmaier 16y ago
Another: I solved the 8-queens problem for a magazine contest using a BASIC program (!)
Instead of modelling the board with an 8X8 array, I used the digits 1-8 representing the height of the queen (row #), and the 8 digits in a string as the heights for each column. I permuted the digits - n! time to execute which was never going to finish.
So, prune! as it swapped digits left to right in a Grey-code-like sequence, test the digits to the left for diagonals (|row#A - row#B| == |A-B|) and don't bother to recurse if its already failing.
Every few minutes - out popped a solution, and it finished in under 25 minutes.
Nowadays, that would take imperceptible time to execute.