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

→‎{{header|Pascal}}: updated speed by only change the new elements in the middle of the row before.
m (→‎{{header|Python}}: : arbitrary matrix size printing)
(→‎{{header|Pascal}}: updated speed by only change the new elements in the middle of the row before.)
Line 884:
 
=={{header|Pascal}}==
Using symmetry within row and col.Fill only the middle and let the values before in place.
The values of a row y start with 0 and increment by one until col x = minimum(y,MaxCoor-y)<BR>
Using symmetry within row, to only calc til the middle.
<lang pascal>program mindistance;
{$IFDEF FPC} //used fpc 3.2.1
Line 902 ⟶ 901:
var
dgtwidth : NativeUint;
resOneRowElems : tMinDist;
 
function CalcDigitWidth(n: NativeUint):NativeUint;
begin
result:= 12;
while n>= 10 do
Begin
Line 914 ⟶ 913:
end;
 
procedure OutOneRow(var resOneRowElems:tMinDist);
var
one_digit,one_row :string;
Line 920 ⟶ 919:
begin
one_row:= '';
For i := low(resOneRowElems) to High(resOneRowElems) do
begin
str(resOneRowElems[i]:dgtwidth,one_digit);
one_row += one_digit;
end;
Line 931 ⟶ 930:
var
pRes : tpMinDist;
col,min_rowmin_dist,row : NativeInt;
begin
//iniated with 0
setlength(res,MaxCoor);
setlength(OneRowElems,MaxCoor);
pRes := @res[0];
MaxCoor -= 1;//= High(OneRowElems);
pRes := @OneRowElems[0];
 
row := 0MaxCoor;
repeat
min_rowmin_dist := MaxCoor-row;
if min_rowmin_dist > row then
min_rowmin_dist := row;
//fill the inner rest with min_dist
//increment until min_row-1 is reached
FillDWord(pRes[min_dist],(MaxCoor-2*min_dist+1),min_dist);
For col := 0 to min_row-1 do
begin
pRes[col] := col;
//mirror_col
pRes[MaxCoor-col] := col;
end;
//fill the rest with min_row
// for col := MaxCoor-min_row downto min_row do pRes[col] := min_row;
FillDWord(pRes[min_row],(MaxCoor-2*min_row+1),min_row);
 
OutOneRow(resOneRowElems);
 
inc(row);
until row> MaxCoordec(row);
until row < 0;
writeln;
setlength(resOneRowElems,0);
end;
 
Line 971 ⟶ 964:
 
Begin
// Test(200*1000);// without output TIO.RUN Real time: 4.152 s CPU share: 97.70 %
Test(23);
Test(10);
Test(9);
Test(1);
end.
Test(23);
</lang>
// Test(200000);// without output 8.34s == 4e10/8.34s/3.7e9 = 1.3 cpucycles/cell
end.</lang>
{{out}}
<pre style="width: 800px; height: 480px>
<pre>
TIO.RUN
Minimum number of cells after, before, above and below 23 x 23 square:
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 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 10 x 10 square:
0 0 0 0 0 0 0 0 0 0
0000000000
0 1 1 1 1 1 1 1 1 0
0111111110
0 1 2 2 2 2 2 2 1 0
0122222210
0 1 2 3 3 3 3 2 1 0
0123333210
0 1 2 3 4 4 3 2 1 0
0123443210
0 1 2 3 4 4 3 2 1 0
0123443210
0 1 2 3 3 3 3 2 1 0
0123333210
0 1 2 2 2 2 2 2 1 0
0122222210
0 1 1 1 1 1 1 1 1 0
0111111110
0 0 0 0 0 0 0 0 0 0
0000000000
 
Minimum number of cells after, before, above and below 9 x 9 square:
0 0 0 0 0 0 0 0 0
000000000
0 1 1 1 1 1 1 1 0
011111110
0 1 2 2 2 2 2 1 0
012222210
0 1 2 3 3 3 2 1 0
012333210
0 1 2 3 4 3 2 1 0
012343210
0 1 2 3 3 3 2 1 0
012333210
0 1 2 2 2 2 2 1 0
012222210
0 1 1 1 1 1 1 1 0
011111110
0 0 0 0 0 0 0 0 0
000000000
 
Minimum number of cells after, before, above and below 1 x 1 square:
0
</pre>
 
Minimum number of cells after, before, above and below 23 x 23 square:
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 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 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