結果
問題 | No.217 魔方陣を作ろう |
ユーザー | rpy3cpp |
提出日時 | 2015-05-26 23:49:21 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 32 ms / 5,000 ms |
コード長 | 1,716 bytes |
コンパイル時間 | 80 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-07-06 10:30:20 |
合計ジャッジ時間 | 1,487 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
10,880 KB |
testcase_01 | AC | 32 ms
10,880 KB |
testcase_02 | AC | 31 ms
10,880 KB |
testcase_03 | AC | 31 ms
10,880 KB |
testcase_04 | AC | 31 ms
10,880 KB |
testcase_05 | AC | 31 ms
10,880 KB |
testcase_06 | AC | 31 ms
11,008 KB |
testcase_07 | AC | 31 ms
10,880 KB |
testcase_08 | AC | 31 ms
10,880 KB |
testcase_09 | AC | 31 ms
10,880 KB |
testcase_10 | AC | 31 ms
10,880 KB |
testcase_11 | AC | 30 ms
10,880 KB |
testcase_12 | AC | 30 ms
10,880 KB |
testcase_13 | AC | 31 ms
10,880 KB |
testcase_14 | AC | 30 ms
10,752 KB |
testcase_15 | AC | 31 ms
10,880 KB |
testcase_16 | AC | 30 ms
10,880 KB |
testcase_17 | AC | 31 ms
10,752 KB |
ソースコード
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()