魔术可能吗?


18

幻方是一个正乘n正方形网格,在范围填充有不同的正整数1,2,...,N ^ 2,使得每个单元包含每个行中的不同的整数和整数的和,列和对角线相等。

您的任务是获取由正数和空单元格的占位符组成的n × n矩阵(我将使用 0,但您可以使用任何喜欢的非数字字符或数据类型),并确定是否为填写遗漏的数字可以制作一个魔术方块

矩阵将至少为2×2,最多为10×10。最小的非平凡的幻方是3×3。输入矩阵中的数字可能大于n ^ 2,并且所有单元格都有可能被填充。

测试用例:

2   2
2   0
False

8   0   6
0   5   0
0   9   2
True

16    2    3   13
 5   11   10    8
 9    7    6   12
 4   14   15    1
True

10   0   1
 0   5   9
 3   7   5
False

99    40    74     8    15    51     0    67     0     1
 0    41    55    14     0    57    64     0    98     0
81    47    56    20    22    63    70    54     0    88
 0    28     0    21     0    69    71    60    85    19
 0    34     0     2     9    75    52    61     0    25
24    65    49     0    90    26    33    42    17    76
 0     0    30    89    91     0    39    48     0    82
 6    72    31    95     0    38    45    29     0    13
12    53     0    96    78     0     0     0    10    94
18    59    43    77     0     0    27    36     0   100
True

嗯 我想我已经看到了解决方案的地方..
马修卢武铉

1
建议的测试用例,以确保正确测试了对角线:([ [ 1, 5, 9 ], [ 6, 7, 2 ], [ 8, 3, 4 ] ]虚假)
Arnauld

我们可以给占位符编号(即[[8, X1, 6], [X2, 5, X3], [X4, 9, 2]])吗?
斯科特·米尔纳

@Scott,请放心……
Stewie Griffin

Answers:


4

JavaScript(ES6),270 268字节

将矩阵作为2D数组。返回01

a=>(g=(x,y=0,w=a.length,p,R=a[y])=>[0,1,2,3].some(d=>a.some((r,y)=>(p=s)^(s=r.reduce((p,v,x)=>(o|=1<<(v=[v,(b=a[x])[y],b[x++],b[w-x]][d]),p+v),0))&&p),s=o=0)||o/2+1!=1<<w*w?R&&[...Array(w*w)].map((_,n)=>(p=R[x])==++n|!p&&(R[x]=n,g(z=(x+1)%w,y+!z),R[x]=p)):r=1)(r=0)&&r

测试用例

对于最后一个测试案例,这绝对太慢了。:-(


2

05AB1E,45 个字节

Zsgn©>‹®L¹˜Kœ0ªε\¹˜0y.;¹gô©O®øO®Å\O®Å/O)˜Ë}à*

也使用 0作为占位符。越多0s(或上面的数字 ñ2)中的输入,程序运行速度会变慢。矩阵的大小无关紧要(一个10x10的矩阵,其中三个0s的运行速度比具有7的3x3矩阵快很多 0s)。

可能少了4个字节,但是目前.;2D列表内置的bug 。:并且.:按预期方式工作,但.;没有做二维名单什么现在..因此变通的˜¹gô扁平化矩阵; .;在清单上使用;并再次将其转换回矩阵。

在线尝试验证更多测试用例。(注意:不包含质询描述的最后一个测试用例,因为它的0太多了。)

说明:

Z               # Get the maximum of the (implicit) input-matrix (implicitly flattened)
                # (and without popping the matrix)
                #  i.e. [[8,0,6],[0,5,0],[0,0,2]] → 8
 s              # Swap to get the input-matrix again
  g             # Get its length (amount of rows)
                #  i.e. [[8,0,6],[0,5,0],[0,0,2]] → 3
   n            # Square it
                #  i.e. 3 → 9
    ©           # Store it in the register (without popping)
     >‹         # Check if the maximum is <= this squared matrix-dimension
                #  i.e. 8 <= 9 → 1 (truthy)
®               # Push the squared matrix-dimension again
 L              # Create a list in the range [1, squared_matrix_dimension]
                #  i.e. 9 → [1,2,3,4,5,6,7,8,9]
  ¹             # Push the input-matrix
   ˜            # Flatten it
                #  i.e. [[8,0,6],[0,5,0],[0,0,2]] → [8,0,6,0,5,0,0,0,2]
    K           # Remove all these numbers from the ranged list
                #  i.e. [1,2,3,4,5,6,7,8,9] and [8,0,6,0,5,0,0,0,2] → [1,3,4,7,9]
œ               # Get all possible permutations of the remaining numbers
                # (this part is the main bottleneck of the program;
                #  the more 0s and too high numbers, the more permutations)
                #   i.e. [1,3,4,7,9] → [[1,3,4,7,9],[1,3,4,9,7],...,[9,7,4,1,3],[9,7,4,3,1]]
 0ª             # Add an item 0 to the list (workaround for inputs without any 0s)
                #  i.e. [[1,3,4,7,9],[1,3,4,9,7],...,[9,7,4,1,3],[9,7,4,3,1]] 
                #   → [[1,3,4,7,9],[1,3,4,9,7],...,[9,7,4,1,3],[9,7,4,3,1],"0"] 
   ε            # Map each permutation `y` to:
    \           #  Remove the implicit `y` which we don't need yet
    ¹˜          #  Push the flattened input again
      0         #  Push a 0
       y        #  Push permutation `y`
        .;      #  Replace all 0s with the numbers in the permutation one by one
                #   i.e. [8,0,6,0,5,0,0,0,2] and [1,3,4,7,9]
                #    → [8,1,6,3,5,4,7,9,2]
          ¹g    #  Push the input-dimension again
            ô   #  And split the flattened list into parts of that size,
                #  basically transforming it back into a matrix
                #   i.e. [8,1,6,3,5,4,7,9,2] and 3 → [[8,1,6],[3,5,4],[7,9,2]]
             ©  #  Save the matrix with all 0s filled in in the register (without popping)
    O           #  Take the sum of each row
                #   i.e. [[8,1,6],[3,5,4],[7,9,2]] → [15,12,18]
    ®øO         #  Take the sum of each column
                #   i.e. [[8,1,6],[3,5,4],[7,9,2]] → [18,15,12]
    ®Å\O        #  Take the sum of the top-left to bottom-right main diagonal
                #   i.e. [[8,1,6],[3,5,4],[7,9,2]] → 15
    ®Å/O        #  Take the sum of the top-right to bottom-left main diagonal
                #   i.e. [[8,1,6],[3,5,4],[7,9,2]] → 18
    )           #  Wrap everything on the stack into a list
                #   → [[15,12,18],[18,15,12],15,18]
     ˜          #  Flatten it
                #   i.e. [[15,12,18],[18,15,12],15,18] → [15,12,18,18,15,12,15,18]
      Ë         #  Check if all values are equal
                #   i.e. [15,12,18,18,15,12,15,18] → 0 (falsey)
}               # After the map:
                #  → [0,0,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,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,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0]
 à              # Check if any are truthy by taking the maximum
                #  → 1 (truthy)
  *             # And multiply the two checks to verify both are truthy
                #  i.e. 1 and 1 → 1 (truthy)
                # (and output the result implicitly)

我的05AB1E回答中©O®øO®Å\O®Å/O)˜Ë也使用了该部分,以供“ 验证魔术方”挑战使用,因此请参阅该回答以获取有关该部分代码的更深入的说明。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.