Skip to content

Instantly share code, notes, and snippets.

@kentan
Created February 17, 2014 06:26
Show Gist options
  • Save kentan/9045674 to your computer and use it in GitHub Desktop.
Save kentan/9045674 to your computer and use it in GitHub Desktop.
my $size = $ARGV[0];
my $count = 0;
sub run {
(my $v,my $arr) = @_;
if($v == $size){
$count++;
return 1;
}
my $h = 0;
while($h < $size){
my $i = 0;
for($i = $v - 1; $i >=0; $i--){
if ($$arr[$i] == $h ||
$$arr[$i] == $h - ($v - $i) ||
$$arr[$i] == $h + ($v - $i)){
last;
}
}
if ($i != -1){
$h++;
next;
}
$$arr[$v] = $h;
$rv = run($v + 1, \@$arr);
if($rv == -1){
$$arr[$v] = -1;
}
$h++;
}
return 1 if($v == 0 && $h == $size);
return -1;
}
run(0,\@arr2);
print $count,"\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment