마술이 가능합니까?


18

매직 스퀘어 인 N-의해-N 범위에있는 별개의 양의 정수로 채워진 정사각형 그리드 ^ 2, 2, ..., N , 각 셀은 각 행에서 다른 정수와 정수의 합을 포함 이러한 열과 대각선이 같습니다.

당신의 임무는 양수와 빈 셀에 대한 자리 표시 자 문자로 구성된 nxn 행렬 을 취하고 ( 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

흠. 어딘가에서 해결책을 본 것 같습니다.
— Matthew Roh

1
대각선이 올바르게 테스트되도록 제안 된 테스트 사례 : [ [ 1, 5, 9 ], [ 6, 7, 2 ], [ 8, 3, 4 ] ](거짓)
— Arnauld

자리 표시 자에 번호를 지정할 수 있습니까 (예 :) [[8, X1, 6], [X2, 5, X3], [X4, 9, 2]]?
— Scott Milner

@Scott 확실히, 부담없이 ...
— Stewie Griffin

답변:


4

자바 스크립트 (ES6) 270 268 바이트

행렬을 2D 배열로 사용합니다. 반환 0또는 1.

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)˜Ë}à*

00엔200

4 바이트가 줄어들었을 수도 있지만 현재 .;2D 목록이 있는 내장 버그가 있습니다 . :그리고 .:예상하지만, 작품으로 .;주위의 작업 .. 지금 따라서를 2D 목록에 아무것도하지 않는 ˜및 ¹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)

이 부분 ©O®øO®Å\O®Å/O)˜Ë은 Verify Magic Square 챌린지에 대한 내 05AB1E 답변 에도 사용 되므로 해당 부분에 대한 자세한 설명은 해당 답변을 참조하십시오.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.