0466253
2020/09/06(日) 00:11:00.10ID:Lo+HJN6n($H, $W) = split' ',<DATA>; @a = map{[split/\s*/]} <DATA>;
for $h (0..$H-1) { for $w (0..$W-1) {
$c = $a[$h][$w];
$S = "$h,$w" if $c eq 'S'; $G = "$h,$w" if $c eq 'G';
if ($c ne '#') {
$e{($h-1).",$w $h,$w"}++ if $h and $a[$h-1][$w] =~ /[.SG]/;
$e{"$h,".($w-1)." $h,$w"}++ if $w and $a[$h][$w-1] =~ /[.SG]/;
}}}
use feature qw{current_sub say}; no warnings 'experimental';
sub {
my $p = @_[-1];
for my $q (map{s/ *$p *//; $_} grep{/$p/} keys %e) {
if ($q eq $G) { $h{"@{[@_, $q]}"}++ }
else { __SUB__->(@_, $q) unless grep{/$q/} @_ }
}
}->($S);
say scalar keys %h;
__DATA__
5 5
S....
##.#.
...#.
...#.
....G
実行結果
~ $ perl 18_380_maze_paths.pl
12