Rather obfuscated and brute force (updated to handle more than 8 queens):

#!/usr/bin/perl -w use strict; my $N= @ARGV ? $ARGV[0] : 8; my $max= $N - 1; my $zero= ""; vec( $zero, $max, 1 )= 0; my $chars= length($zero); my $board= $zero x $N; my @col= map { my $c= $zero; vec($c,$_,1)= 1; $c } 0..$max; my $down= $board . join "", @col; my $up= $board . join "", reverse @col; my %pos; @pos{@col}= 0..$max; my $sols= 0; sub AddQueen { my( $q, $board, $sol )= @_; if( $q == $N ) { $sols++; warn Xform($sol), " solution $sols: @pos{ $sol =~ /.{$chars}/gos }\n"; return; } my $row= substr($board,$q*$chars,$chars); for my $bit ( @col ) { if( $zero eq ( $bit & $row ) ) { AddQueen( $q+1, $board | $bit x $N | substr( $up, (2*$N-1-$pos{$bit}-$q)*$chars ) | substr( $down, ($N+$pos{$bit}-$q)*$chars ), $sol.$bit ); } } } my $uniq= 0; my %uniq; sub Xform { my( $sol )= @_; return "Duplicate" if $uniq{$sol}; $uniq++; $uniq{$sol}++; # Identity my( @sol )= @pos{ $sol =~ /.{$chars}/gos }; my( @los )= map {$max-$_} @sol; # For - mirror $uniq{join "", @col[reverse @sol]}++; # | mirror $uniq{join "", @col[@los]}++; # - mirror $uniq{join "", @col[reverse @los]}++; # -| = 180 turn @sol[@sol]= 0..$max; # For \ mirror @los[reverse @los]= 0..$max; # Add |\ = -|\ = / $uniq{join "", @col[@sol]}++; # \ mirror $uniq{join "", @col[reverse @sol]}++; # \| = +90 turn $uniq{join "", @col[@los]}++; # / mirror $uniq{join "", @col[reverse @los]}++; # /| = -90 turn return "Unique"; } AddQueen( 0, $board, "" ); warn "Total of $uniq unique solutions (in ", time()-$^T, " secs).\n";

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Efficient N-Queen solution with Perl by tye
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.