11
Objective: Given a matrix of positive integers \$M\$, output the smallest centrosymmetric matrix which contains \$M\$ (this matrix may contain non-positive integers as well).
A centrosymmetric matrix is a square matrix with rotational symmetry of order 2—i.e. it remains the same matrix after rotating it twice. For example, a centrosymmetric matrix has the top-left element the same as the bottom-right, and the element above the center the same as the one below the center. A useful visualization can be found here.
More formally, given a matrix \$M\$, produce a square matrix \$N\$ such that \$N\$ is centrosymmetric and \$M\subseteq N\$, and there is no other square matrix \$K\$ such that \$\dim K<\dim N\$.
\$A\$ is a subset of \$B\$ (notation: \$A\subseteq B\$) if and only if each value \$A_{i,j}\$ appears at index \$B_{i+i^\prime,j+j^\prime}\$ for some pair of integers \$(i^\prime, j^\prime)\$.
Note: some matrices have multiple solutions (e.g. [[3,3],[1,2]]
being solved as [[2,1,0],[3,3,3],[0,1,2]]
or [[3,3,3],[1,2,1],[3,3,3]]
); you must output at least one of the valid solutions.
Test cases
input
example output
[[1, 2, 3],
[4, 5, 6]]
[[1, 2, 3, 0],
[4, 5, 6, 0],
[0, 6, 5, 4],
[0, 3, 2, 1]]
[[9]]
[[9]]
[[9, 10]]
[[9, 10],
[10, 9]]
[[100, 200, 300]]
[[100, 200, 300],
[ 0, 0, 0],
[300, 200, 100]]
[[1, 2, 3],
[4, 5, 4]]
[[1, 2, 3],
[4, 5, 4]
[3, 2, 1]]
[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]
[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]
[[4, 5, 4],
[1, 2, 3]]
[[3, 2, 1],
[4, 5, 4],
[1, 2, 3]]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1]]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
Why do Centrosymmetric matrices have to be square? – Post Rock Garf Hunter – 2018-08-06T05:24:05.720
@WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition – Conor O'Brien – 2018-08-06T05:28:53.827
I was wondering why you made that choice – Post Rock Garf Hunter – 2018-08-06T05:29:19.977
2@WW it was a simplification I thought to be useful for clarity – Conor O'Brien – 2018-08-06T05:30:39.227