Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Re: The N-queens problem using pure regexes

by benizi (Hermit)
on Oct 10, 2003 at 19:58 UTC ( [id://298393]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: The N-queens problem using pure regexes
in thread The N-queens problem using pure regexes

For any odd N odd N not divisible by 3: $a_solution = [ map { chr(ord('a') + $_ - 1).(((2 * $_) - 1) % $N); } (1..$N) ]

(e.g., for $N = 11: [ a1, b3, c5, d7, e9, f11, g2, h4, i6, j8, k10 ])

This is equivalent to starting in the bottom-left corner of the board and moving right one square and up two squares (wrapping when you hit the edge) N-1 times.

If the regex picks the first space not-already-capturable in each column (From brief inspection, it appears to do so -- It finds an equivalent solution for odd-N), this is the first solution it will find. In the even-N case, this process will not leave any not-already-capturable squares in the last column on the first pass, so it must then backtrack.

Update: Whoa there, Ben. I spoke way too soon. The above solution only applies when N is odd AND not divisible by 3. (So, for N = (1,5) mod 6)

More update: I was wrong about most of the analysis, too. This won't be the first solution found.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://298393]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-24 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found