結果

問題 No.223 1マス指定の魔方陣
ユーザー lam6er
提出日時 2025-03-31 17:38:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,378 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 68,228 KB
最終ジャッジ日時 2025-03-31 17:39:26
合計ジャッジ時間 4,250 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16 WA * 10 RE * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def rotate(matrix, times=1):
    n = len(matrix)
    rotated = [row.copy() for row in matrix]
    for _ in range(times % 4):
        new_rotated = []
        for j in range(n):
            new_row = [rotated[n - i - 1][j] for i in range(n)]
            new_rotated.append(new_row)
        rotated = new_rotated
    return rotated

def flip_horizontal(matrix):
    return [row[::-1] for row in matrix]

def flip_vertical(matrix):
    return [row.copy() for row in reversed(matrix)]

def complement(matrix):
    n = len(matrix)
    return [[(n*n +1) - matrix[i][j] for j in range(n)] for i in range(n)]

def generate_variations(base):
    variations = []
    for r in range(4):
        rotated = rotate(base, r)
        for flip in ['none', 'h', 'v', 'both']:
            if flip == 'h':
                flipped = flip_horizontal(rotated)
            elif flip == 'v':
                flipped = flip_vertical(rotated)
            elif flip == 'both':
                flipped = flip_horizontal(flip_vertical(rotated))
            else:
                flipped = [row.copy() for row in rotated]
            variations.append(flipped)
            comp_flipped = complement(flipped)
            variations.append(comp_flipped)
    return variations

def main():
    input_line = sys.stdin.read().split()
    N = int(input_line[0])
    X = int(input_line[1])
    Y = int(input_line[2])
    Z = int(input_line[3])

    target_row = Y - 1
    target_col = X - 1

    initial_magics = [
        # Base magic square 1
        [
            [1, 15, 14, 4],
            [12, 6, 7, 9],
            [8, 10, 11, 5],
            [13, 3, 2, 16]
        ],
        # Complement of base magic square 1
        [
            [16, 2, 3, 13],
            [5, 11, 10, 8],
            [9, 7, 6, 12],
            [4, 14, 15, 1]
        ],
        # Example magic square from the output
        [
            [3, 13, 16, 2],
            [10, 8, 5, 11],
            [6, 12, 9, 7],
            [15, 1, 4, 14]
        ]
    ]

    # For N=4, check all transformations of initial magic squares
    for base in initial_magics:
        variations = generate_variations(base)
        for var in variations:
            if var[target_row][target_col] == Z:
                for row in var:
                    print(' '.join(map(str, row)))
                return

if __name__ == "__main__":
    main()
0