Minimum number of cells after, before, above and below NxN squares: Difference between revisions

m
→‎{{header|Perl}}: adapt for multiple input N
m (→‎{{header|Perl}}: adapt for multiple input N)
Line 1,031:
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perluse strict;
 
use strict; # https://rosettacode.org/wiki/Minimum_number_of_cells_after,_before,_above_and_below_NxN_squares
use warnings;
use List::Util qw( max min );
 
for my $N =(0, 1, 2, 6, 9, 23) 10;{
my $fmt = ('%' . (1+length int $N/2) . 'd') x $N . "\n";
 
print "$N x $N distance to nearest edge:\n";
for my $row ( 0 .. $N - 1 ) {
{
my @cols = map { min $_, $row, $N-1 - max $_, $row } 0 .. $N-1;
print " printf $fmt, @cols\n";
}</lang>
print "\n";
}</lang>
{{out}}
<pre>0 x 0 distance to nearest edge:
<pre>
 
0 0 0 0 0 0 0 0 0 0
0 1 1x 1 1distance 1to 1 1 1nearest 0edge:
0
0 1 2 2 2 2 2 2 1 0
 
0 1 2 3 3 3 3 2 1 0
2 x 2 distance to nearest edge:
0 1 2 3 4 4 3 2 1 0
0 0
0 1 2 3 4 4 3 2 1 0
0 0
0 1 2 3 3 3 3 2 1 0
 
0 1 2 2 2 2 2 2 1 0
6 x 6 distance to nearest edge:
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0
</pre>
0 1 2 2 1 0
0 1 2 2 1 0
0 1 1 1 1 0
0 0 0 0 0 0
 
9 x 9 distance to nearest edge:
0 0 0 0 0 0 0 0 0 0
0 1 21 21 21 21 2 21 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 43 3 2 1 0
0 1 2 32 32 3 32 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
23 x 23 distance to nearest edge:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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