結果

問題 No.217 魔方陣を作ろう
ユーザー maspymaspy
提出日時 2020-03-04 16:56:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,650 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 44,500 KB
最終ジャッジ日時 2024-04-22 01:42:51
合計ジャッジ時間 9,774 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 461 ms
43,732 KB
testcase_01 WA -
testcase_02 AC 457 ms
43,732 KB
testcase_03 AC 465 ms
43,732 KB
testcase_04 AC 457 ms
44,000 KB
testcase_05 WA -
testcase_06 AC 469 ms
43,988 KB
testcase_07 AC 464 ms
43,988 KB
testcase_08 AC 475 ms
43,992 KB
testcase_09 WA -
testcase_10 AC 476 ms
43,736 KB
testcase_11 AC 483 ms
44,116 KB
testcase_12 AC 479 ms
43,984 KB
testcase_13 WA -
testcase_14 AC 479 ms
43,992 KB
testcase_15 AC 486 ms
44,248 KB
testcase_16 AC 462 ms
44,116 KB
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
import numpy as np

N = int(read())

# %%


def f(N):
    if N % 4 == 0:
        return f0(N)
    elif N % 4 == 2:
        return f2(N)
    else:
        return f13(N)


def f13(N):
    A = np.zeros((N, N), np.int32)
    x = 0
    y = N // 2
    for i in range(1, N * N + 1):
        if A[x][y]:
            x += 2
            y -= 1
            x %= N
            y %= N
        A[x, y] = i
        x -= 1
        y += 1
        x %= N
        y %= N
    return A


def f0(N):
    A = np.arange(N * N).reshape(N, N)
    
    A[1::4, 0::4] *= -1
    A[1::4, 3::4] *= -1
    A[2::4, 0::4] *= -1
    A[2::4, 3::4] *= -1
    A[0::4, 1::4] *= -1
    A[0::4, 2::4] *= -1
    A[3::4, 1::4] *= -1
    A[3::4, 1::4] *= - 1
    A[A < 0] += N * N - 1
    A += 1
    return A


def f2(N):
    A = f(N // 2)
    A = np.repeat(A, 2, axis=0).repeat(2, axis=1)
    A = (A - 1) * 4
    n = N // 4
    # L-type
    A[:2 * n + 2:2, ::2] += 4
    A[:2 * n + 2:2, 1::2] += 1
    A[1:2 * n + 2:2, ::2] += 2
    A[1: 2 * n + 2:2, 1::2] += 3
    # U-type
    A[2 * n + 2, ::2] += 1
    A[2 * n + 2, 1::2] += 4
    A[2 * n + 3, ::2] += 2
    A[2 * n + 3, 1::2] += 3
    # X-type
    A[2 * n + 4::2, ::2] += 1
    A[2 * n + 4::2, 1::2] += 4
    A[2 * n + 5::2, ::2] += 3
    A[2 * n + 5::2, 1::2] += 2
    # modify center
    A[2 * n, 2 * n] -= 3
    A[2 * n, 2 * n + 1] += 3
    A[2 * n + 2, 2 * n] += 3
    A[2 * n + 2, 2 * n + 1] -= 3
    return A


# %%
A = f(N)
print('\n'.join(' '.join(row) for row in A.astype(str)))
0