結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 31 ms
11,136 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 30 ms
11,008 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 31 ms
11,008 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 31 ms
11,008 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 30 ms
11,008 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 = (i + 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