Minimum number of cells after, before, above and below NxN squares: Difference between revisions
Alextretyak (talk | contribs) (Added 11l) |
(Added XPL0 example.) |
||
Line 2,353: | Line 2,353: | ||
Minimum number of cells after, before, above and below 1 x 1 square: |
Minimum number of cells after, before, above and below 1 x 1 square: |
||
0 |
0 |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<lang XPL0>func Min(A, B); |
|||
int A, B; |
|||
return if A<B then A else B; |
|||
def N=10, M=N-1, C=M/2; |
|||
int X, Y, VX, VY; |
|||
[for Y:= 0 to M do |
|||
[for X:= 0 to M do |
|||
[VX:= if X <= C then X else M-X; |
|||
VY:= if Y <= C then Y else M-Y; |
|||
IntOut(0, Min(VX, VY)); ChOut(0, ^ ); |
|||
]; |
|||
CrLf(0); |
|||
]; |
|||
]</lang> |
|||
{{out}} |
|||
<pre> |
|||
0 0 0 0 0 0 0 0 0 0 |
|||
0 1 1 1 1 1 1 1 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 4 3 2 1 0 |
|||
0 1 2 3 3 3 3 2 1 0 |
|||
0 1 2 2 2 2 2 2 1 0 |
|||
0 1 1 1 1 1 1 1 1 0 |
|||
0 0 0 0 0 0 0 0 0 0 |
|||
</pre> |
</pre> |
Revision as of 16:04, 20 February 2022
- Task
Find and show on this page the minimum number of cells after, before, above and below N×N squares, where N = 10.
11l
<lang 11l>F min_cells_matrix(siz)
R (0 .< siz).map(row -> (0 .< @siz).map(col -> min(@row, col, @@siz - @row - 1, @@siz - col - 1)))
F display_matrix(mat)
V siz = mat.len V spaces = I siz < 20 {2} E I siz < 200 {3} E 4 print("\nMinimum number of cells after, before, above and below "siz‘ x ’siz‘ square:’) L(row) 0 .< siz print(mat[row].map(n -> String(n).rjust(@spaces)).join(‘’))
L(siz) [23, 10, 9, 2, 1]
display_matrix(min_cells_matrix(siz))</lang>
- Output:
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 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0
ALGOL 68
As with the Algol W version, the cells are printed as they are calculated. Ensures the counts are shown in the same width. <lang algol68>BEGIN # show the minimum number of cells above, below, before and after each #
# cell in a suare matrix #
PROC min = ( INT a, b )INT: IF a < b THEN a ELSE b FI;
PROC print min cells = ( INT n )VOID: BEGIN # deduce how many digits we need to show so the counts are all # # the same width # INT w = BEGIN INT width := 1, v := ( ( n - ( ABS NOT ODD n ) ) OVER 2 ); WHILE v > 9 DO v OVERAB 10; width +:= 1 OD; width END; print( ( "Minimum number of cells after, before, above and below " , whole( n, 0 ) , " x " , whole( n, 0 ) , " square:" , newline ) ); FOR r FROM 0 TO n - 1 DO FOR c FROM 0 TO n - 1 DO print( ( whole( min( n-r-1, min( r, min( c, n-c-1 ) ) ), -w ), " " ) ) OD; print( ( newline ) ) OD END # print min cells # ; []INT tests = ( 10, 9, 2, 1, 21 ); FOR i FROM LWB tests TO UPB tests DO print min cells( tests[ i ] ); print( ( newline ) ) OD
END</lang>
- Output:
Minimum number of cells after, before, above and below 10 x 10 square: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0 Minimum number of cells after, before, above and below 21 x 21 square: 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 0 0 1 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 2 1 0 0 1 2 3 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 4 3 2 1 0 0 1 2 3 4 5 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 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 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 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 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 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 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 5 4 3 2 1 0 0 1 2 3 4 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 3 2 1 0 0 1 2 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 1 0 0 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
ALGOL W
This version avoids generating an explicit list of elements for each row in the matrix and just prints the elements as they are calculated. The elements are all shown in the same field width. <lang algolw>begin % show the minimum number of cells above, below, before and after each %
% cell in a square matrix %
integer procedure min4( integer value a, b, c, d ) ; begin integer m; m := a; if b < m then m := b; if c < m then m := c; if d < m then m := d; m end min4 ;
procedure printMinCells ( integer value n ) ; begin integer w, v; w := 1; v := ( ( n - ( if odd( n ) then 1 else 0 ) ) div 2 ); while v > 9 do begin v := v div 10; w := w + 1 end; write( i_w := 1, s_w := 0, "Minimum number of cells after, before, above and below ", n, " x ", n, " square:" ); write(); for r := 0 until n - 1 do begin for c := 0 until n - 1 do writeon( i_w := w, s_w := 1, min4( n-r-1, r, c, n-c-1 ) ); write() end for_r end printMinCells ; for n := 10, 9, 2, 1 do printMinCells( n )
end.</lang>
- Output:
Minimum number of cells after, before, above and below 10 x 10 square: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0 Minimum number of cells after, before, above and below 21 x 21 square: 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 0 0 1 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 2 1 0 0 1 2 3 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 4 3 2 1 0 0 1 2 3 4 5 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 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 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 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 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 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 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 5 4 3 2 1 0 0 1 2 3 4 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 3 2 1 0 0 1 2 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 1 0 0 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
APL
<lang apl>n_by_n ← (⌽⌊⊖)∘(∘.⌊⍨¯1+⍳) n_by_n¨ 2 3 9 10</lang>
- Output:
┌───┬─────┬─────────────────┬───────────────────┐ │0 0│0 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 0│0 1 1 1 1 1 1 1 0│0 1 1 1 1 1 1 1 1 0│ │ │0 0 0│0 1 2 2 2 2 2 1 0│0 1 2 2 2 2 2 2 1 0│ │ │ │0 1 2 3 3 3 2 1 0│0 1 2 3 3 3 3 2 1 0│ │ │ │0 1 2 3 4 3 2 1 0│0 1 2 3 4 4 3 2 1 0│ │ │ │0 1 2 3 3 3 2 1 0│0 1 2 3 4 4 3 2 1 0│ │ │ │0 1 2 2 2 2 2 1 0│0 1 2 3 3 3 3 2 1 0│ │ │ │0 1 1 1 1 1 1 1 0│0 1 2 2 2 2 2 2 1 0│ │ │ │0 0 0 0 0 0 0 0 0│0 1 1 1 1 1 1 1 1 0│ │ │ │ │0 0 0 0 0 0 0 0 0 0│ └───┴─────┴─────────────────┴───────────────────┘
AutoHotkey
<lang AutoHotkey>gridSize := 10
grid := [] loop % gridSize {
row := A_Index loop % gridSize { col := A_Index grid[row, col] := Min(row, col, gridSize+1-row, gridSize+1-col) - 1 }
}
for row, obj in Grid {
for col, v in obj result .= v " " result .= "`n"
} MsgBox % result</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
AWK
<lang AWK>
- syntax: GAWK -f MINIMUM_NUMBER_OF_CELLS_AFTER_BEFORE_ABOVE_AND_BELOW_NXN_SQUARES.AWK
BEGIN {
leng = split("3,4,9,10,23",arr,",") for (k=1; k<=leng; k++) { n = arr[k] printf("\nDistance to nearest edge: %dx%d\n",n,n) for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { printf("%2d ",min(min(i-1,n-i),min(j-1,n-j))) } printf("\n") } } exit(0)
} function min(x,y) { return((x < y) ? x : y) } </lang>
- Output:
Distance to nearest edge: 3x3 0 0 0 0 1 0 0 0 0 Distance to nearest edge: 4x4 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 Distance to nearest edge: 9x9 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Distance to nearest edge: 10x10 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Distance to nearest edge: 23x23 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
BASIC
BASIC256
<lang BASIC256>function min(a, b) if a<=b then return a else return b end function
subroutine minab(n) for i = 1 to n for j = 1 to n print min(min(i-1, n-i), min(j-1, n-j)); " "; next j print next i end subroutine
call minab(10) end</lang>
- Output:
Igual que la entrada de FreeBASIC.
QBasic
<lang QBasic>DECLARE FUNCTION min! (a!, b!) DECLARE SUB minab (n!)
CLS minab (10) END
FUNCTION min (a, b) IF a <= b THEN min = a ELSE min = b END FUNCTION
SUB minab (n) FOR i = 1 TO n FOR j = 1 TO n PRINT USING "## "; min(min(i - 1, n - i), min(j - 1, n - j)); NEXT j PRINT NEXT i END SUB</lang>
- Output:
Igual que la entrada de FreeBASIC.
True BASIC
<lang qbasic>FUNCTION min (a, b)
IF a <= b THEN LET min = a ELSE LET min = b
END FUNCTION
SUB minab (n)
FOR i = 1 TO n FOR j = 1 TO n PRINT USING "## ": min(min(i - 1, n - i), min(j - 1, n - j)); NEXT j PRINT NEXT i
END SUB
CALL minab (10) END</lang>
- Output:
Igual que la entrada de QBasic.
Yabasic
<lang yabasic>minab(10) end
sub minab(n) for i = 1 to n for j = 1 to n print min(min(i-1, n-i), min(j-1, n-j)) using ("##"); next j print next i end sub</lang>
- Output:
Igual que la entrada de FreeBASIC.
BCPL
<lang bcpl>get "libhdr"
let min(a,b) = a a, b
let minNbyN(n, cw) be
for y=0 to n-1 $( for x=0 to n-1 do writed(min(x, min(n-x-1, min(y, n-y-1))), cw) wrch('*N') $)
let start() be minNbyN(10, 3)</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
BQN
<lang bqn>NByN ← ⌊⌜˜ ⌽⊸⌊∘↕ NByN¨ 2‿3‿9‿10</lang>
- Output:
┌─ · ┌─ ┌─ ┌─ ┌─ ╵ 0 0 ╵ 0 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 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 ┘ 0 0 0 0 1 2 2 2 2 2 1 0 0 1 2 2 2 2 2 2 1 0 ┘ 0 1 2 3 3 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 4 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 4 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 ┘ 0 0 0 0 0 0 0 0 0 0 ┘ ┘
C
<lang c>#include<stdio.h>
- include<stdlib.h>
- define min(a, b) (a<=b?a:b)
void minab( unsigned int n ) {
int i, j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf( "%2d ", min( min(i, n-1-i), min(j, n-1-j) )); } printf( "\n" ); } return;
}
int main(void) {
minab(10); return 0;
}</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
CLU
<lang clu>min = proc [T: type] (a, b: T) returns (T)
where T has lt: proctype (T,T) returns (bool) if a<b then return(a) else return(b) end
end min
min_n_by_n = proc (n: int) returns (array[array[int]])
ai = array[int] aai = array[ai] t: aai := aai$[] for y: int in int$from_to(0, n-1) do aai$addh(t, ai$[]) for x: int in int$from_to(0, n-1) do i: int := min[int](x, min[int](n-x-1, min[int](y, n-y-1))) ai$addh(aai$top(t), i) end end return(t)
end min_n_by_n
print_table = proc (s: stream, table: array[array[int]])
ai = array[int] aai = array[ai] for line: ai in aai$elements(table) do for item: int in ai$elements(line) do stream$puts(s, int$unparse(item) || " ") end stream$putl(s, "") end
end print_table
start_up = proc ()
print_table(stream$primary_output(), min_n_by_n(10))
end start_up</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
COBOL
<lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. MINIMUM-CELLS-N-BY-N. DATA DIVISION. WORKING-STORAGE SECTION. 01 TABLE-SIZE PIC 99 VALUE 10. 01 X PIC 99. 01 Y PIC 99. 01 ITEM PIC 99. 01 MIN PIC 99. 01 FMT PIC ZZ9. 01 TABLE-LINE PIC X(72). 01 LINE-PTR PIC 99. PROCEDURE DIVISION. BEGIN. PERFORM MAKE-LINE VARYING Y FROM 0 BY 1 UNTIL Y IS EQUAL TO TABLE-SIZE. STOP RUN. MAKE-LINE. MOVE SPACES TO TABLE-LINE. MOVE 1 TO LINE-PTR. PERFORM ADD-ITEM VARYING X FROM 0 BY 1 UNTIL X IS EQUAL TO TABLE-SIZE. DISPLAY TABLE-LINE. ADD-ITEM. PERFORM FIND-MINIMUM-VALUE. MOVE MIN TO FMT. STRING FMT DELIMITED BY SIZE INTO TABLE-LINE WITH POINTER LINE-PTR. FIND-MINIMUM-VALUE. MOVE X TO MIN. MOVE Y TO ITEM. PERFORM CHECK-MINIMUM. COMPUTE ITEM = TABLE-SIZE - Y - 1. PERFORM CHECK-MINIMUM. COMPUTE ITEM = TABLE-SIZE - X - 1. PERFORM CHECK-MINIMUM. CHECK-MINIMUM. IF ITEM IS LESS THAN MIN, MOVE ITEM TO MIN.</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
Excel
LAMBDA
Binding the name distancesToEdge to the following lambda expression in the Name Manager of the Excel WorkBook:
(See LAMBDA: The ultimate Excel worksheet function)
<lang lisp>=LAMBDA(n,
LET( lastIndex, n - 1, LAMBDA(i, LET( x, MOD(i, n), y, QUOTIENT(i, n), evaluate( "MIN({" & TEXT(x, "0") & "," & TEXT(y, "0") & "," & TEXT(lastIndex - x, "0") & "," & TEXT(lastIndex - y, "0") & "})" ) ) )(SEQUENCE(n, n, 0, 1)) )
)</lang>
- Output:
The single formula in the cell B2 defines the whole matrix value which spills out to column K and row 11:
fx | =distancesToEdge(A2) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | ||
1 | Dimension | |||||||||||
2 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
3 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | ||
4 | 0 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 0 | ||
5 | 0 | 1 | 2 | 3 | 3 | 3 | 3 | 2 | 1 | 0 | ||
6 | 0 | 1 | 2 | 3 | 4 | 4 | 3 | 2 | 1 | 0 | ||
7 | 0 | 1 | 2 | 3 | 4 | 4 | 3 | 2 | 1 | 0 | ||
8 | 0 | 1 | 2 | 3 | 3 | 3 | 3 | 2 | 1 | 0 | ||
9 | 0 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 0 | ||
10 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | ||
11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
12 | ||||||||||||
13 | 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
14 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | |||
15 | 0 | 1 | 2 | 2 | 2 | 2 | 2 | 1 | 0 | |||
16 | 0 | 1 | 2 | 3 | 3 | 3 | 2 | 1 | 0 | |||
17 | 0 | 1 | 2 | 3 | 4 | 3 | 2 | 1 | 0 | |||
18 | 0 | 1 | 2 | 3 | 3 | 3 | 2 | 1 | 0 | |||
19 | 0 | 1 | 2 | 2 | 2 | 2 | 2 | 1 | 0 | |||
20 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | |||
21 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |||
22 | ||||||||||||
23 | 2 | 0 | 0 | |||||||||
24 | 0 | 0 | ||||||||||
25 | ||||||||||||
26 | 1 | 0 |
F#
<lang fsharp> // Minimum number of cells after, before, above and below NxN squares. Nigel Galloway: August 1st., 2021 printfn "%A" (Array2D.init 10 10 (fun n g->List.min [n;g;9-n;9-g])) printfn "\n%A" (Array2D.init 9 9 (fun n g->List.min [n;g;8-n;8-g])) </lang>
- Output:
[[0; 0; 0; 0; 0; 0; 0; 0; 0; 0] [0; 1; 1; 1; 1; 1; 1; 1; 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; 4; 3; 2; 1; 0] [0; 1; 2; 3; 3; 3; 3; 2; 1; 0] [0; 1; 2; 2; 2; 2; 2; 2; 1; 0] [0; 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; 1; 1; 1; 1; 1; 1; 1; 0] [0; 1; 2; 2; 2; 2; 2; 1; 0] [0; 1; 2; 3; 3; 3; 2; 1; 0] [0; 1; 2; 3; 4; 3; 2; 1; 0] [0; 1; 2; 3; 3; 3; 2; 1; 0] [0; 1; 2; 2; 2; 2; 2; 1; 0] [0; 1; 1; 1; 1; 1; 1; 1; 0] [0; 0; 0; 0; 0; 0; 0; 0; 0]]
Factor
<lang factor>USING: io kernel math math.matrices math.vectors prettyprint sequences ;
- square ( n -- matrix )
[ <cartesian-square-indices> ] keep 1 - dup '[ dup sum _ > [ _ v-n vabs ] when infimum ] matrix-map ;
{ 10 9 2 1 } [ square simple-table. nl ] each</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 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 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Fermat
<lang fermat> Func Min(a, b) = if a<=b then a else b fi.; n:=10; Array x[n, n]; [x]:= [<i=1,n> <j=1,n> Min(Min(i-1,n-i),Min(j-1,n-j))]; [x]; </lang>
- Output:
[[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, `
0, 1, 1, 1, 1, 1, 1, 1, 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, 4, 3, 2, 1, 0, ` 0, 1, 2, 3, 3, 3, 3, 2, 1, 0, ` 0, 1, 2, 2, 2, 2, 2, 2, 1, 0, ` 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, ` 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]]
FreeBASIC
<lang freebasic>#define min(a, b) Iif(a<=b,a,b)
sub minab( n as uinteger )
for i as uinteger = 1 to n for j as uinteger = 1 to n print using "## ";min( min(i-1, n-i), min(j-1, n-j) ); next j print next i
end sub
minab(10)</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
Go
<lang go>package main
import "fmt"
func printMinCells(n int) {
fmt.Printf("Minimum number of cells after, before, above and below %d x %d square:\n", n, n) p := 1 if n > 20 { p = 2 } for r := 0; r < n; r++ { cells := make([]int, n) for c := 0; c < n; c++ { nums := []int{n - r - 1, r, c, n - c - 1} min := n for _, num := range nums { if num < min { min = num } } cells[c] = min } fmt.Printf("%*d \n", p, cells) }
}
func main() {
for _, n := range []int{23, 10, 9, 2, 1} { printMinCells(n) fmt.Println() }
}</lang>
- Output:
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] [0 1 1 1 1 1 1 1 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 4 3 2 1 0] [0 1 2 3 3 3 3 2 1 0] [0 1 2 2 2 2 2 2 1 0] [0 1 1 1 1 1 1 1 1 0] [0 0 0 0 0 0 0 0 0 0] Minimum number of cells after, before, above and below 9 x 9 square: [0 0 0 0 0 0 0 0 0] [0 1 1 1 1 1 1 1 0] [0 1 2 2 2 2 2 1 0] [0 1 2 3 3 3 2 1 0] [0 1 2 3 4 3 2 1 0] [0 1 2 3 3 3 2 1 0] [0 1 2 2 2 2 2 1 0] [0 1 1 1 1 1 1 1 0] [0 0 0 0 0 0 0 0 0] Minimum number of cells after, before, above and below 2 x 2 square: [0 0] [0 0] Minimum number of cells after, before, above and below 1 x 1 square: [0]
GW-BASIC
<lang gwbasic>10 N = 10 20 FOR I = 0 TO N - 1 30 IF I < N - 1 - I THEN DI = I ELSE DI = N - 1 - I 40 FOR J = 0 TO N - 1 50 IF J < N - 1 - J THEN DJ = J ELSE DJ = N - 1 - J 60 IF DI < DJ THEN M = DI ELSE M = DJ 70 PRINT USING "## ";M; 80 NEXT J 90 PRINT 100 NEXT I</lang>
Haskell
<lang haskell>import Data.List.Split (chunksOf)
SHORTEST DISTANCES TO EDGE OF MATRIX ---------
distancesToEdge :: Int -> Int distancesToEdge n =
( \i -> chunksOf n $ (\(x, y) -> minimum [x, y, i - x, i - y]) <$> (fmap (,) >>= (<*>)) [0 .. i] ) $ pred n
TEST -------------------------
main :: IO () main =
mapM_ putStrLn $ showMatrix . distancesToEdge <$> [10, 9, 2, 1]
DISPLAY ------------------------
showMatrix :: Show a => a -> String showMatrix m =
let w = (succ . maximum) $ fmap (length . show) =<< m rjust n c = (drop . length) <*> (replicate n c <>) in unlines (unwords . fmap (rjust w ' ' . show) <$> m)</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 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 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J
<lang j>nByN=: (|."1<.|.)@(<./~@i.) nByN each 2 3 9 10</lang>
- Output:
┌───┬─────┬─────────────────┬───────────────────┐ │0 0│0 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 0│0 1 1 1 1 1 1 1 0│0 1 1 1 1 1 1 1 1 0│ │ │0 0 0│0 1 2 2 2 2 2 1 0│0 1 2 2 2 2 2 2 1 0│ │ │ │0 1 2 3 3 3 2 1 0│0 1 2 3 3 3 3 2 1 0│ │ │ │0 1 2 3 4 3 2 1 0│0 1 2 3 4 4 3 2 1 0│ │ │ │0 1 2 3 3 3 2 1 0│0 1 2 3 4 4 3 2 1 0│ │ │ │0 1 2 2 2 2 2 1 0│0 1 2 3 3 3 3 2 1 0│ │ │ │0 1 1 1 1 1 1 1 0│0 1 2 2 2 2 2 2 1 0│ │ │ │0 0 0 0 0 0 0 0 0│0 1 1 1 1 1 1 1 1 0│ │ │ │ │0 0 0 0 0 0 0 0 0 0│ └───┴─────┴─────────────────┴───────────────────┘
Julia
<lang julia>function printNbyN(sizes)
for N in sizes mat = zeros(Int, N, N) println("\n\nMinimum number of cells after, before, above and below $N x $N square:") for r in 1:N, c in 1:N mat[r, c] = min(r - 1, c - 1, N - r, N - c) end display(mat) end
end
printNbyN([23, 10, 9, 2, 1])
</lang>
- Output:
Minimum number of cells after, before, above and below 23 x 23 square: 23×23 Matrix{Int64}: 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: 10×10 Matrix{Int64}: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 9×9 Matrix{Int64}: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 2×2 Matrix{Int64}: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 1×1 Matrix{Int64}: 0
MiniZinc
<lang MiniZinc> %Minimum number of cells after, before, above and below NxN squares. Nigel Galloway, August 3rd., 2021 int: Size=10; int: S=Size-1; set of int: N=0..S; array[N,N] of var int: G = array2d(N,N,[min([n,g,S-n,S-g])|n,g in N]); output([show2d(G)]) </lang>
- Output:
[| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | 0, 1, 1, 1, 1, 1, 1, 1, 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, 4, 3, 2, 1, 0 | 0, 1, 2, 3, 3, 3, 3, 2, 1, 0 | 0, 1, 2, 2, 2, 2, 2, 2, 1, 0 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |] ---------- Finished in 209msec
Modula-2
<lang modula2>MODULE MinNByN; FROM InOut IMPORT WriteCard, WriteLn;
PROCEDURE minNbyN(n, cellwidth: CARDINAL);
VAR x, y: CARDINAL; PROCEDURE min(a, b: CARDINAL): CARDINAL; BEGIN IF a < b THEN RETURN a; ELSE RETURN b; END; END min;
BEGIN
FOR y := 0 TO n-1 DO FOR x := 0 TO n-1 DO WriteCard(min(x, min(n-x-1, min(y, n-y-1))), cellwidth); END; WriteLn(); END;
END minNbyN;
BEGIN
minNbyN(10, 3);
END MinNByN.</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
Nim
<lang Nim>import strutils
proc printMinCells(n: Positive) =
echo "Minimum number of cells after, before, above and below $1 x $1 square:".format(n) var cells = newSeq[int](n) for r in 0..<n: for c in 0..<n: cells[c] = min([n - r - 1, r, c, n - c - 1]) echo cells.join(" ")
when isMainModule:
for n in [10, 9, 2, 1]: printMinCells(n) echo()</lang>
- Output:
Minimum number of cells after, before, above and below 10 x 10 square: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0
PARI/GP
<lang parigp> n=10 matrix(n,n,i,j,min(min(i-1,n-i),min(j-1,n-j))) </lang>
- Output:
[0 0 0 0 0 0 0 0 0 0]
[0 1 1 1 1 1 1 1 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 4 3 2 1 0]
[0 1 2 3 3 3 3 2 1 0]
[0 1 2 2 2 2 2 2 1 0]
[0 1 1 1 1 1 1 1 1 0]
[0 0 0 0 0 0 0 0 0 0]
Pascal
Using symmetry within row and col.Fill only the middle and let the values before in place. <lang pascal>program mindistance; {$IFDEF FPC} //used fpc 3.2.1
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF} uses
sysutils
{$IFDEF WINDOWS},Windows{$ENDIF}
;
type
tMinDist = array of Uint32; tpMinDist= pUint32;
var
dgtwidth : NativeUint; OneRowElems : tMinDist;
function CalcDigitWidth(n: NativeUint):NativeUint; begin
result:= 2; while n>= 10 do Begin inc(result); n := n DIV 10; end;
end;
procedure OutOneRow(var OneRowElems:tMinDist); var
one_digit,one_row :string; i : NativeInt;
begin
one_row:= ; For i := low(OneRowElems) to High(OneRowElems) do begin str(OneRowElems[i]:dgtwidth,one_digit); one_row += one_digit; end; writeln(one_row);
end;
procedure OutSquareDist(MaxCoor : NativeUInt); var
pRes : tpMinDist; min_dist,row : NativeInt;
begin
//iniated with 0 setlength(OneRowElems,MaxCoor); MaxCoor -= 1;//= High(OneRowElems); pRes := @OneRowElems[0];
row := MaxCoor; repeat min_dist := MaxCoor-row; if min_dist > row then min_dist := row; //fill the inner rest with min_dist FillDWord(pRes[min_dist],(MaxCoor-2*min_dist+1),min_dist);
OutOneRow(OneRowElems);
dec(row); until row < 0; writeln; setlength(OneRowElems,0);
end;
procedure Test(MaxCoor:NativeInt); begin
if MaxCoor<= 0 then EXIT; write('Minimum number of cells after, before, above and below '); writeln(MaxCoor,' x ',MaxCoor,' square:'); dgtwidth := CalcDigitWidth(NativeUint(MaxCoor) DIV 2); OutSquareDist(MaxCoor);
end;
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. </lang>
- Output:
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 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0
Perl
<lang perl>use strict; use warnings; use List::Util qw( max min );
for my $N (0, 1, 2, 6, 9, 23) {
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; printf $fmt, @cols; } print "\n";
}</lang>
- Output:
0 x 0 distance to nearest edge: 1 x 1 distance to nearest edge: 0 2 x 2 distance to nearest edge: 0 0 0 0 6 x 6 distance to nearest edge: 0 0 0 0 0 0 0 1 1 1 1 0 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 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 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 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
Phix
with javascript_semantics procedure distance_to_edge(integer n) printf(1,"Minimum number of cells after, before, above and below %d x %d square:\n",n) for r=1 to n do for c=1 to n do printf(1,"%2d",min({r-1,c-1,n-r,n-c})) end for printf(1,"\n") end for end procedure papply({23,10,9,2,1},distance_to_edge)
- Output:
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 9101010 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9101110 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9101010 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 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0
Although I rather like it the way it is, you could argue there should be more spacing on the 23x23, if you insist do this before the loops and use fmt on the innermost line:
string fmt = sprintf("%%%dd",length(sprint(floor((n-1)/2)))+1)
or maybe just (good for n<=200 whereas the above goes on and on to "%4d", etc.)
string fmt = iff(n<=20?"%2d":"%3d")
PILOT
<lang pilot>C :size=10
:y=0
- line
C :$l=
:x=0
- item
C :sy=(size-y)-1
:sx=(size-x)-1 :i=x
C (y<i):i=y C (sy<i):i=sy C (sx<i):i=sx
:$l=$l #i :x=x+1
J (x<size):*item T :$l C :y=y+1 J (y<size):*line E :</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
Python
<lang python>def min_cells_matrix(siz):
return [[min(row, col, siz - row - 1, siz - col - 1) for col in range(siz)] for row in range(siz)]
def display_matrix(mat):
siz = len(mat) spaces = 2 if siz < 20 else 3 if siz < 200 else 4 print(f"\nMinimum number of cells after, before, above and below {siz} x {siz} square:") for row in range(siz): print("".join([f"{n:{spaces}}" for n in mat[row]]))
def test_min_mat():
for siz in [23, 10, 9, 2, 1]: display_matrix(min_cells_matrix(siz))
if __name__ == "__main__":
test_min_mat()
</lang>
- Output:
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 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0
Or, disentangling computation from IO (separating model from display), and composing from generics:
<lang python>Distance to edge of matrix
from itertools import chain, product
- distancesToEdge :: Int -> Int
def distancesToEdge(n):
A square matrix of dimension n, in which each value is the minimum distance from the matrix position to the edge of the matrix. lastIndex = n - 1 axis = range(0, n) return chunksOf(n)([ min(x, y, lastIndex - x, lastIndex - y) for (x, y) in product(axis, axis) ])
- ------------------------- TEST -------------------------
- main :: IO ()
def main():
Square matrices of distances to the matrix edge. Sample matrices of dimensions [10, 9, 2, 1]. print('\n\n'.join([ showMatrix(distancesToEdge(n)) for n in [10, 9, 2, 1] ]))
- ----------------------- DISPLAY ------------------------
- showMatrix :: Int -> String
def showMatrix(xs):
String representation of xs as a matrix. def go(): rows = [[str(x) for x in row] for row in xs] w = max(map(len, chain.from_iterable(rows))) return "\n".join( " ".join(k.rjust(w, ' ') for k in row) for row in rows ) return go() if xs else
- ----------------------- GENERIC ------------------------
- chunksOf :: Int -> [a] -> a
def chunksOf(n):
A series of lists of length n, subdividing the contents of xs. Where the length of xs is not evenly divisible, the final list will be shorter than n. def go(xs): return [ xs[i:n + i] for i in range(0, len(xs), n) ] if 0 < n else None return go
- MAIN ---
if __name__ == '__main__':
main()</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 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 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Raku
<lang perl6>sub distance-to-edge (\N) {
my $c = ceiling N / 2; my $f = floor N / 2; my @ul = ^$c .map: -> $x { [ ^$c .map: { min($x, $_) } ] } @ul[$_].append: reverse @ul[$_; ^$f] for ^$c; @ul.push: [ reverse @ul[$_] ] for reverse ^$f; @ul
}
for 0, 1, 2, 6, 9, 23 {
my @dte = .&distance-to-edge; my $max = chars max flat @dte».Slip; say "\n$_ x $_ distance to nearest edge:"; .fmt("%{$max}d").say for @dte;
}</lang>
- Output:
0 x 0 distance to nearest edge: 1 x 1 distance to nearest edge: 0 2 x 2 distance to nearest edge: 0 0 0 0 6 x 6 distance to nearest edge: 0 0 0 0 0 0 0 1 1 1 1 0 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 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 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 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
REXX
This REXX version automatically adjusts the width of each (cell) number displayed so that all displayed numbers are aligned. <lang rexx>/*REXX pgm finds the minimum# of cells after, before, above, & below a NxN square matrix*/ parse arg $ /*obtain optional arguments from the CL*/ if $= | $="," then $= 21 10 9 2 1 /*Not specified? Then use the default.*/
@title= ' the minimum number of cells after, before, above, and below a ' do j=1 for words($); g= word($, j) /*process each of the squares specified*/ w= length( (g-1) % 2) /*width of largest number to be shown. */ say center(@title g"x"g ' square matrix ', 86) /*center title of output to be shown. */ say center(, 86, '─') /*display a separator line below title.*/
do r=0 for g /*process output for a NxN sq. matrix*/ _= left(, max(0, 85%(w+1) -g ) ) /*compute indentation output centering.*/ do c=0 for g _= _ right( min(r, c, g-r-1, g-c-1), w) /*construct a row of the output matrix.*/ end /*c*/ say _ /*display a row of the output square. */ end /*r*/
say; say /*display 2 blank lines between outputs*/ end /*j*/ /*stick a fork in it, we're all done. */</lang>
- output when using the default inputs:
the minimum number of cells after, before, above, and below a 21x21 square matrix ────────────────────────────────────────────────────────────────────────────────────── 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 0 0 1 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 2 1 0 0 1 2 3 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 4 3 2 1 0 0 1 2 3 4 5 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 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 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 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 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 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 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 5 4 3 2 1 0 0 1 2 3 4 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 3 2 1 0 0 1 2 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 1 0 0 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 the minimum number of cells after, before, above, and below a 10x10 square matrix ────────────────────────────────────────────────────────────────────────────────────── 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 the minimum number of cells after, before, above, and below a 9x9 square matrix ────────────────────────────────────────────────────────────────────────────────────── 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 the minimum number of cells after, before, above, and below a 2x2 square matrix ────────────────────────────────────────────────────────────────────────────────────── 0 0 0 0 the minimum number of cells after, before, above, and below a 1x1 square matrix ────────────────────────────────────────────────────────────────────────────────────── 0
Ring
<lang ring> see "working..." + nl see "Minimum number of cells after, before, above and below NxN squares:" + nl row = 0 cellsMin = []
for n = 1 to 10
for m = 1 to 10 cells = [] add(cells,m-1) add(cells,10-m) add(cells,n-1) add(cells,10-n) min = min(cells) add(cellsMin,min) next
next
ind = 100 for n = 1 to ind
row++ see "" + cellsMin[n] + " " if row%10 = 0 see nl ok
next
see "done..." + nl </lang>
- Output:
working... Minimum number of cells after, before, above and below NxN squares: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 done...
Ruby
<lang ruby>def dist2edge(n)
width = (n/2).to_s.size+1 m = n-1 (0..m).map do |x| (0..m).map{|y| [x, y, m-x, m-y].min.to_s.center(width) }.join end
end
puts dist2edge(10)</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
Wren
<lang ecmascript>import "/math" for Nums import "/fmt" for Fmt
var printMinCells = Fn.new { |n|
System.print("Minimum number of cells after, before, above and below %(n) x %(n) square:") var p = (n < 21) ? 1 : 2 for (r in 0...n) { var cells = List.filled(n, 0) for (c in 0...n) cells[c] = Nums.min([n-r-1, r, c, n-c-1]) Fmt.print("$*d", p, cells) }
}
for (n in [23, 10, 9, 2, 1]) {
printMinCells.call(n) System.print()
}</lang>
- Output:
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 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 9 x 9 square: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 3 3 2 1 0 0 1 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Minimum number of cells after, before, above and below 2 x 2 square: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 0
XPL0
<lang XPL0>func Min(A, B); int A, B; return if A<B then A else B;
def N=10, M=N-1, C=M/2; int X, Y, VX, VY; [for Y:= 0 to M do
[for X:= 0 to M do [VX:= if X <= C then X else M-X; VY:= if Y <= C then Y else M-Y; IntOut(0, Min(VX, VY)); ChOut(0, ^ ); ]; CrLf(0); ];
]</lang>
- Output:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 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 4 3 2 1 0 0 1 2 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0