I like this kind of problem. Here's my solution, which finds the 92 solutions for an 8x8 board in under a second (or will solve any NxN board, given enough stack space and time, it blows up for me with "Deep recursion" errors for me on 100x100...), but does not collapse the identical solutions due to rotation and reflection to 12. That's an excercise for someone else :-)
The output is simply the column (or row) for each queen in each succeeding row (or column). It's rather brute force, but sped up slighty from blind brute force by using a hash array. I think its very idiomatic perl, it'd be interesting to compare against the java version:
use strict; use warnings; my $n = shift || 8; my @board; # Initialize board for my $i (1..$n) { for my $j (1..$n) { $board[$i]{$j} = 1; } } my @positions; put_q(1); sub put_q { my $i = shift; for my $j (keys %{$board[$i]}) { if ($i == $n) { print "@positions[1..$#positions] $j\n"; } else { $positions[$i] = $j; my $marked = mark_board($i, $j); put_q($i+1); unmark_board($marked); } } } sub mark_board { my ($i, $j) = @_; my $j1 = my $j2 = $j; my @marked; for my $x ($i+1..$n) { push @marked, [$x, $j1] if --$j1 >= 1 and delete $board[$x]{$j1}; push @marked, [$x, $j2] if ++$j2 <= $n and delete $board[$x]{$j2}; push @marked, [$x, $j] if delete $board[$x]{$j}; } return \@marked; } sub unmark_board { $board[$_->[0]]{$_->[1]} = 1 for @{$_[0]}; }
This algorithm is exponential, though an 8x8 board takes less than a second, a 10x10 board took about 11 seconds and a 11x11 board took ~55 seconds. For a 30x30 board, I'm still seeing how long it takes to find even the first solution :)
(Update2: A chart on this page will give you an idea how long it takes to get the first solutions for brute force algorithms when you change N).

In reply to Re: Efficient N-Queen solution with Perl by runrig
in thread Efficient N-Queen solution with Perl by lestrrat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.