r/programmingchallenges • • Jul 16 '12
N-Queens: C++
Looking for some help with the N-Queens puzzle. I have a recursive solution that works reasonably well for N <= 10, but it's not fast enough (Solve for N = 13 in < 5 seconds) I need to produce all possible combinations and print the queen positions in a certain way. I need a different algorithm than what I have at the moment, any help? I know that I can eliminate quite a lot of board positions by looking at reflections and rotations, but I do not know how to do this. Any Help?
6
Upvotes
View all comments
Show parent comment
1
u/[deleted] Jul 17 '12
Sorry for a double reply... My algorithm is a naive recursive solution. It places a queen in each row/column recursively. If the new board position is still legal, go another level deeper. If the depth == N, that's a solution. If the new board position isn't valid, it returns.