in reply to Efficient N-Queen solution with Perl
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 :)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]}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Efficient N-Queen solution with Perl
by tye (Sage) on Nov 20, 2001 at 05:00 UTC |