20/05/04 17:18:49 7s1fJbf+.net
>>437 Perl5、素直に壁で跳ね返る回数をカウント
for (<DATA>) {
($H, $W, @a) = /(\S+)/g;
@s = map{[map{$_ eq '#'} split '']} @a;
my %t;
for $h (0..$H-1) { $t{$h+0.5}{0} = $t{$h+0.5}{$W} = 1;
for $w (0..$W-1) { $t{0}{$w+0.5} = $t{$H}{$w+0.5} = 1;
next unless $s[$h][$w];
$t{$h}{$w+0.5} = $t{$h+0.5}{$w} = $t{$h+0.5}{$w+1} = $t{$h+1}{$w+0.5} = 1;
}
}
($x, $y, $dx, $dy, $n) = (0.5, 0, 0.5, 0.5, 0);
do {
$x += $dx; $y += $dy;
if ($t{$y}{$x}) {
$dx = -$dx if $y =~ /\.5/; $dy = -$dy if $x =~ /\.5/;
$n++;
}
} while $x != 0.5 or $y != 0;
print "$n\n"
}
__DATA__
1 1 .
4 5 ..#.. #..#. .#..# ..#..
5 10 .......... #########. .......... .######### ..........
実行結果
~ $ perl 17_437_bounce.pl
4
18
66