Skip to content

Instantly share code, notes, and snippets.

@libitte
Created April 17, 2013 00:16
Show Gist options
  • Save libitte/5400747 to your computer and use it in GitHub Desktop.
Save libitte/5400747 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
my @board = ();
solve(0);
sub solve {
my ($num) = @_;
my $row;
my $column;
for my $row (0..7){
for my $column (0..$num-1) {
if ($board[$column] == $row) {
goto RETRY;
}
elsif (($column - $board[$column]) == ($num - $row)){
goto RETRY;
}
elsif (($column + $board[$column]) == ($num + $row)){
goto RETRY;
}
}
$board[$num] = $row;
if ($num == 8-1) {
my $column = 0;
for $column (0..7) {
printf("%d", $board[$column]);
}
print "\n";
}
else {
solve($num+1);
}
RETRY:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment