Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: The N-queens problem using pure regexesby Abigail-II (Bishop) |
on Oct 11, 2003 at 01:34 UTC ( [id://298462]=note: print w/replies, xml ) | Need Help?? |
It has to do with how many positions are rejected before
a suitable one is found. The solutions found for n = 8 and
n = 9 are:
As you can see, for n = 8, it never has to backtrack for the first queen (a8 is choosen), but for the seconde queen, b8, b7, b6, and b5 need to be rejected. b8 and b7 will be rejected right away (as they are attacked by a8), but for b6 and b5 to be rejected, lots of other queens will be have to be placed. For n = 9, no backtracking for the first queen is needed, and for the second queen, the positions b9 and b8 are rejected immediately. It's only the third queen were there's some real backtracking going on - c9, c8, c7, and c6 are rejected immediately, and only for c5 more queens will be tried before rejecting it. The timings for 'faster' with n >= 10 cannot be trusted, as the program contained a bug for n >= 10 (see elsewhere in this thread - the bug is now fixed). Here's a new table (done on a different computer, and recording user times, not wall clock time), with the fixed programs:
And, in case you are interested, the code that generated the table:
Home work question: the code above is lacking something vital. What is it not doing what it should do? Abigail
In Section
Meditations
|
|