N Queens Puzzle Algorithm

Discussion of chess software programming and technical issues.

Moderator: Ras

Fulvio
Posts: 399
Joined: Fri Aug 12, 2016 8:43 pm

Re: N Queens Puzzle Algorithm

Post by Fulvio »

AlvaroBegue wrote: It's instructive to understand where this code is getting slow
I tried to make the code even more understandable:
https://wandbox.org/permlink/N4tAIVSqyN4X4dkC

It is super simple:
- the permutation loop tries all the valid boards with exactly one queen in each row and column.
- each valid board is rotated by 45 degrees (just plus and minus math) to transform diagonals in row and column
- if there is only one queen for row and column it is a valid solution.

I thought the algorithm could be interesting because it is simple and because, having no state and consisting of a simple rotation, it may be adapted to run on parallel hardware.

I looked at the rosetta code link, and there are clever and faster algorithm, like the C one. Impressive, however using information from previous calculations makes them inherently sequential and in my opinion that path do not have much room for improvements.