-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueens.pl
More file actions
50 lines (40 loc) · 1.29 KB
/
nqueens.pl
File metadata and controls
50 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/perl
use strict;
use warnings;
sub get_next; # returns an array of valid boards containing another queen
my $n; # size of board
# get $n
if (@ARGV < 1) {
print "n? ";
$n = <STDIN>;
chomp $n;
} else {
$n = $ARGV[0];
}
my @solutions = get_next;
foreach my $i (1 .. $n-1) { # keep adding queens until we have n of them
my @temp_solutions;
push (@temp_solutions, get_next @$_) foreach (@solutions);
@solutions = @temp_solutions;
}
if (@solutions == 0) {
print "No solutions exist.\n";
} else {
print ($_ + 1, ": ", join(", ", @{$solutions[$_]}), "\n") foreach (0 .. $#solutions);
}
sub get_next {
my @rows_available = (1) x $n; # boolean array storing which rows can validly have a queen
foreach (0 .. $#_) {
my $row = $_[$_];
$rows_available[$row] = 0; # can't place a queen in the same row
my $pos_diag = $row + @_ - $_; # can't place a queen in the positive...
my $neg_diag = $row - @_ + $_; # ...or negative diagonal
$rows_available[$pos_diag] = 0 if ($pos_diag < $n);
$rows_available[$neg_diag] = 0 if ($neg_diag >= 0);
}
my @boards; # array of valid boards with another queen
foreach (0 .. $#rows_available) { # create a new board (array of rows) for each available row
push @boards, [@_, $_] if ($rows_available[$_]);
}
return @boards;
}