結果

問題 No.217 魔方陣を作ろう
ユーザー rpy3cpprpy3cpp
提出日時 2015-05-26 23:49:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,716 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 11,088 KB
実行使用メモリ 8,432 KB
最終ジャッジ日時 2023-09-20 15:24:11
合計ジャッジ時間 1,663 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,384 KB
testcase_01 AC 17 ms
8,116 KB
testcase_02 AC 16 ms
8,368 KB
testcase_03 AC 17 ms
8,416 KB
testcase_04 AC 17 ms
8,028 KB
testcase_05 AC 17 ms
8,424 KB
testcase_06 AC 16 ms
8,432 KB
testcase_07 AC 17 ms
8,356 KB
testcase_08 AC 17 ms
8,368 KB
testcase_09 AC 18 ms
8,368 KB
testcase_10 AC 17 ms
8,364 KB
testcase_11 AC 17 ms
8,428 KB
testcase_12 AC 18 ms
8,344 KB
testcase_13 AC 17 ms
8,336 KB
testcase_14 AC 17 ms
8,420 KB
testcase_15 AC 17 ms
8,344 KB
testcase_16 AC 17 ms
8,376 KB
testcase_17 AC 17 ms
8,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    N = int(input())
    if N & 1:
        magic = solve_odd(N)
    elif N % 4 == 0:
        magic = solve_4(N)
    else:
        magic = solve_LUX(N)
    print_mat(magic)

def print_mat(magic):
    for row in magic:
        print(' '.join(map(str,row)))

def solve_odd(N):
    magic = [[0] * N for i in range(N)]
    mid = N // 2
    num = 1
    x = 0
    y = mid
    while num <= N * N:
        while magic[x][y]:
            x = (x + 2) % N
            y = (y - 1) % N
        magic[x][y] = num
        num += 1
        x = (x - 1) % N
        y = (y + 1) % N
    return magic

def solve_4(N):
    magic = [[0] * N for i in range(N)]
    num = 1
    for x in range(N):
        for y in range(N):
            if is_diagonal(x, y):
                magic[x][y] = num
            num += 1
    num = 1
    for x in range(N-1, -1, -1):
        for y in range(N-1, -1, -1):
            if not magic[x][y]:
                magic[x][y] = num
            num += 1
    return magic

def is_diagonal(x, y):
    x %= 4
    y %= 4
    return x == y or x + y == 3


def solve_LUX(N):
    seed = solve_odd(N//2)
    LUX = [[0] * (N//2) for i in range(1 + N//4)]
    LUX.append([1] * (N//2))
    LUX.extend([[2] * (N//2) for i in range(N//4)])
    LUX[N//4][N//4] = 1
    LUX[1 + N//4][N//4] = 0
    L = [[4, 1], [2, 3]]
    U = [[1, 4], [2, 3]]
    X = [[1, 4], [3, 2]]
    LUXmat = [L, U, X]
    magic = [[0] * N for i in range(N)]
    for x in range(N//2):
        for y in range(N//2):
            val = (seed[x][y] - 1) * 4
            idx = LUX[x][y]
            for dx, dy in [(0, 0), (0, 1), (1, 0), (1, 1)]:
                magic[2 * x + dx][2 * y + dy] = LUXmat[idx][dx][dy] + val
    return magic


solve()
0