I recently got interested in Su DoKu solver. I am testing the three line solver by Edmund von der Burg. I could not understand why the R sub has to die out in order to get the final result. How could I keep the result and use it later?
You can read about the description of the solver at
http://www.ecclestoad.co.uk/blog/.
Here is the code that I am testing:
#!/usr/local/bin/perl
use strict;
use warnings;
use Debug::EchoMessage;
my $self = bless {}, "main";
use integer;
# my @A = split //, <>;
my $q = "";
{local $/; $q = <DATA>; $q =~ s/\s+//g;}
my @A = split //, $q;
# $self->disp_param(\@A);
sub R {
for my $i ( 0 .. 80 ) {
next if $A[$i];
my %t = map {
$_ / 9 == $i / 9 # for row
|| $_ % 9 == $i % 9 # for column
|| $_ / 27 == $i/27 && $_%9/3 == $i%9/3 # for grid
? $A[$_] # $t{$_} = $A[$_]
: 0 => 1 # $t{0} = 1
} 0 .. 80;
R( $A[$i] = $_ ) for grep { !$t{$_} } 1 .. 9;
# $self->disp_param(\@A);
my $j = "";
map {$j .= $A[$_] } 0..80;
print "$j\n";
return $A[$i] = 0;
}
die @A; ### WHY could I just return the result here??
}
R;
my $j = "";
map {$j .= $A[$_] } 0..80;
print "$j\n";
__DATA__
006075400
704921000
010080702
580007903
040000070
302090641
005030090
000102305
008750200
Start Free Trial