in reply to Trying to solve N-Queens
I also think than a single loop might improve performance slightly.sub mark_attacks { my ($r,$c,$array) = @_; $array->[$r]->[$c] = 'Q'; # mark everything my $none; for my $i (1 .. $SIZE) { $none = 1; $none = $array->[$r]->[$c + $i] = 0 if ($c + $i < $SIZE); $none = $array->[$r]->[$c - $i] = 0 if ($c - $i >= 0); $none = $array->[$r + $i]->[$c] = 0 if ($r + $i < $SIZE); $none = $array->[$r - $i]->[$c] = 0 if ($r - $i >= 0); $none = $array->[$r - $i]->[$c - $i] = 0 if ($r - $i >= 0 and $c + - $i >= 0); $none = $array->[$r + $i]->[$c + $i] = 0 if ($r + $i < $SIZE and + $c + $i < $SIZE); $none = $array->[$r - $i]->[$c + $i] = 0 if ($r - $i >= 0 and $c + + $i < $SIZE); $none = $array->[$r + $i]->[$c - $i] = 0 if ($r + $i < $SIZE and + $c - $i >= 0); last if $none; } }
Good luck in your assignment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Trying to solve N-Queens
by Solo (Deacon) on Sep 09, 2002 at 03:53 UTC | |
by fokat (Deacon) on Sep 09, 2002 at 14:13 UTC | |
|
(jeffa) 2Re: Trying to solve N-Queens
by jeffa (Bishop) on Sep 09, 2002 at 19:37 UTC |