8 my ($maxx, $maxy) = (0, 0);
11 push @m1, [ split(//) ];
20 for my $x (0 .. $maxx-1) {
21 for my $y (0 .. $maxy-1) {
22 my $add = int($x*5/$maxx)+int($y*5/$maxy);
23 my $x1 = $x % ($maxx/5);
24 my $y1 = $y % ($maxy/5);
25 $m[$y][$x] = $m1[$y1][$x1] + $add;
26 $m[$y][$x] -= 9 if $m[$y][$x] > 9;
29 my %cost = ("0,0" => 0);
32 while (my $p = shift @todo) {
34 for my $d ([ 0, 1], [0, -1], [1, 0], [-1, 0]) {
35 my $dx = $sx + $d->[0];
36 my $dy = $sy + $d->[1];
37 next if $dx < 0 || $dx >= $maxx || $dy < 0 || $dy >= $maxy;
38 if (!defined $cost{$dx,$dy}
39 || $cost{$dx,$dy} > $cost{$sx,$sy} + $m[$dy][$dx]) {
40 $cost{$dx,$dy} = $cost{$sx,$sy} + $m[$dy][$dx];
41 push @todo, [$dx,$dy];
46 say $cost{$maxx-1,$maxy-1};