結果

問題 No.217 魔方陣を作ろう
ユーザー mkawa2mkawa2
提出日時 2020-01-28 13:58:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 2,581 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-09-15 13:37:14
合計ジャッジ時間 1,561 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,008 KB
testcase_01 AC 32 ms
11,008 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 32 ms
11,008 KB
testcase_04 AC 31 ms
11,136 KB
testcase_05 AC 31 ms
11,008 KB
testcase_06 AC 32 ms
11,136 KB
testcase_07 AC 32 ms
11,008 KB
testcase_08 AC 32 ms
11,008 KB
testcase_09 AC 31 ms
11,136 KB
testcase_10 AC 32 ms
11,008 KB
testcase_11 AC 32 ms
11,008 KB
testcase_12 AC 32 ms
11,008 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 32 ms
11,008 KB
testcase_15 AC 31 ms
11,008 KB
testcase_16 AC 31 ms
10,880 KB
testcase_17 AC 32 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline()[:-1]
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def MF(): return map(float, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LF(): return list(map(float, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
dij = [(0, 1), (1, 0), (0, -1), (-1, 0)]

def odd(n):
    n2 = n ** 2
    res = [[0] * n for _ in range(n)]
    i, j = 0, n // 2
    cnt = 1
    while cnt <= n2:
        res[i][j] = cnt
        cnt += 1
        i = (i - 1) % n
        j = (j + 1) % n
        if res[i][j] > 0: i, j = (i + 2) % n, (j - 1) % n
    return res

def lux(n):
    res = [[0] * n * 2 for _ in range(n * 2)]
    for i in range(0, n, 2):
        for j in range(0, 2 * n, 2):
            res[i][j] = 4
            res[i][j + 1] = 1
            res[i + 1][j] = 2
            res[i + 1][j + 1] = 3
    i = n + 1
    for j in range(0, 2 * n, 2):
        res[i][j] = 1
        res[i][j + 1] = 4
        res[i + 1][j] = 2
        res[i + 1][j + 1] = 3
    for i in range(n + 3, 2 * n, 2):
        for j in range(0, 2 * n, 2):
            res[i][j] = 1
            res[i][j + 1] = 4
            res[i + 1][j] = 3
            res[i + 1][j + 1] = 2
    i, j = n - 1, n - 1
    res[i][j] = 1
    res[i][j + 1] = 4
    res[i + 1][j] = 2
    res[i + 1][j + 1] = 3
    i = n + 1
    res[i][j] = 4
    res[i][j + 1] = 1
    res[i + 1][j] = 2
    res[i + 1][j + 1] = 3
    return res

def main():
    n = II()
    if n % 2:
        ans = odd(n)
    elif n % 4:
        m = n // 2
        b = odd(m)
        b = [[(a - 1) * 4 for a in row] for row in b]
        ans = lux(m)
        for i in range(m):
            for j in range(m):
                bk = b[i][j]
                for di in range(2):
                    for dj in range(2):
                        ans[i * 2 + di][j * 2 + dj] += bk
    else:
        ans = [[0] * n for _ in range(n)]
        for i in range(n):
            for j in range(n):
                if (i - j) % 4 == 0 or (i + j) % 4 == 3:
                    ans[i][j] = i * n + j + 1
        for i in range(n):
            for j in range(n):
                if ans[i][j] == 0:
                    ans[i][j] = n ** 2 - (i * n + j)
    for row in ans:
        print(*row)

main()
0