結果
| 問題 | No.223 1マス指定の魔方陣 |
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 13:48:53 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,429 bytes |
| コンパイル時間 | 432 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 61,420 KB |
| 最終ジャッジ日時 | 2025-06-12 13:49:07 |
| 合計ジャッジ時間 | 4,635 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | WA * 46 |
ソースコード
def generate_magic_square(n):
if n == 4:
return [
[3, 13, 16, 2],
[10, 8, 5, 11],
[6, 12, 9, 7],
[15, 1, 4, 14]
]
else:
arr = [[i * n + j + 1 for j in range(n)] for i in range(n)]
for block_i in range(n // 4):
for block_j in range(n // 4):
start_i = block_i * 4
start_j = block_j * 4
for i in range(4):
for j in range(4):
if (i == j) or (i + j == 3):
original_i = start_i + i
original_j = start_j + j
mirrored_i = start_i + (3 - i)
mirrored_j = start_j + (3 - j)
arr[original_i][original_j], arr[mirrored_i][mirrored_j] = arr[mirrored_i][mirrored_j], arr[original_i][original_j]
return arr
def flip_horizontal(matrix):
return [row[::-1] for row in matrix]
def flip_vertical(matrix):
return matrix[::-1]
def transpose(matrix):
n = len(matrix)
return [[matrix[j][i] for j in range(n)] for i in range(n)]
def rotate_180(matrix):
return flip_vertical(flip_horizontal(matrix))
def rotate_90(matrix):
transposed = transpose(matrix)
return flip_horizontal(transposed)
def rotate_270(matrix):
transposed = transpose(matrix)
return flip_vertical(transposed)
def find_magic_square(n, target_x, target_y, z):
target_y -= 1
target_x -= 1
base = generate_magic_square(n)
reversed_base = [[n*n + 1 - num for num in row] for row in base]
transformations = [
('original', lambda x: x),
('flip_h', flip_horizontal),
('flip_v', flip_vertical),
('transpose', transpose),
('transpose_flip_h', lambda x: flip_horizontal(transpose(x))),
('transpose_flip_v', lambda x: flip_vertical(transpose(x))),
('rotate_180', rotate_180),
('rotate_90', rotate_90),
('rotate_270', rotate_270),
]
for magic in [base, reversed_base]:
for t_name, t_func in transformations:
transformed = t_func([row.copy() for row in magic])
if transformed[target_y][target_x] == z:
return transformed
return base
n, X, Y, Z = map(int, input().split())
magic = find_magic_square(n, X, Y, Z)
for row in magic:
print(' '.join(map(str, row)))
gew1fw