結果

問題 No.283 スライドパズルと魔方陣
ユーザー maspymaspy
提出日時 2020-05-03 16:00:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,268 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 11,436 KB
実行使用メモリ 31,088 KB
最終ジャッジ日時 2023-09-03 14:23:13
合計ジャッジ時間 12,175 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
29,948 KB
testcase_01 AC 134 ms
29,916 KB
testcase_02 AC 132 ms
30,116 KB
testcase_03 WA -
testcase_04 AC 134 ms
30,200 KB
testcase_05 AC 134 ms
30,248 KB
testcase_06 AC 135 ms
30,340 KB
testcase_07 AC 135 ms
30,272 KB
testcase_08 AC 136 ms
30,108 KB
testcase_09 AC 136 ms
30,112 KB
testcase_10 AC 133 ms
30,092 KB
testcase_11 AC 132 ms
30,220 KB
testcase_12 AC 138 ms
30,208 KB
testcase_13 AC 133 ms
30,288 KB
testcase_14 AC 137 ms
30,336 KB
testcase_15 AC 137 ms
30,068 KB
testcase_16 AC 140 ms
30,228 KB
testcase_17 AC 138 ms
30,316 KB
testcase_18 AC 137 ms
30,324 KB
testcase_19 AC 138 ms
30,116 KB
testcase_20 AC 141 ms
30,240 KB
testcase_21 AC 143 ms
30,644 KB
testcase_22 AC 143 ms
30,672 KB
testcase_23 AC 145 ms
30,800 KB
testcase_24 AC 147 ms
30,800 KB
testcase_25 AC 147 ms
30,716 KB
testcase_26 AC 149 ms
30,892 KB
testcase_27 AC 162 ms
31,012 KB
testcase_28 AC 158 ms
31,088 KB
testcase_29 AC 162 ms
30,836 KB
testcase_30 AC 163 ms
30,980 KB
testcase_31 AC 160 ms
30,900 KB
testcase_32 AC 164 ms
30,900 KB
testcase_33 AC 160 ms
30,824 KB
testcase_34 AC 154 ms
30,836 KB
testcase_35 AC 158 ms
30,948 KB
testcase_36 AC 156 ms
30,900 KB
testcase_37 AC 153 ms
30,928 KB
testcase_38 AC 155 ms
30,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def f(N):
    if N % 4 == 0:
        return f0(N)
    elif N % 4 == 2:
        return f2(N)
    else:
        return f13(N)


def f13(N):
    A = np.zeros((N, N), np.int32)
    x = 0
    y = N // 2
    for i in range(1, N * N + 1):
        if A[x][y]:
            x += 2
            y -= 1
            x %= N
            y %= N
        A[x, y] = i
        x -= 1
        y += 1
        x %= N
        y %= N
    return A


def f0(N):
    A = np.arange(N * N).reshape(N, N)

    A[1::4, 0::4] *= -1
    A[1::4, 3::4] *= -1
    A[2::4, 0::4] *= -1
    A[2::4, 3::4] *= -1
    A[0::4, 1::4] *= -1
    A[0::4, 2::4] *= -1
    A[3::4, 1::4] *= -1
    A[3::4, 2::4] *= -1
    A[A < 0] += N * N - 1
    A += 1
    return A


def f2(N):
    A = f(N // 2)
    A = np.repeat(A, 2, axis=0).repeat(2, axis=1)
    A = (A - 1) * 4
    n = N // 4
    # L-type
    A[:2 * n + 2:2, ::2] += 4
    A[:2 * n + 2:2, 1::2] += 1
    A[1:2 * n + 2:2, ::2] += 2
    A[1:2 * n + 2:2, 1::2] += 3
    # U-type
    A[2 * n + 2, ::2] += 1
    A[2 * n + 2, 1::2] += 4
    A[2 * n + 3, ::2] += 2
    A[2 * n + 3, 1::2] += 3
    # X-type
    A[2 * n + 4::2, ::2] += 1
    A[2 * n + 4::2, 1::2] += 4
    A[2 * n + 5::2, ::2] += 3
    A[2 * n + 5::2, 1::2] += 2
    # modify center
    A[2 * n, 2 * n] -= 3
    A[2 * n, 2 * n + 1] += 3
    A[2 * n + 2, 2 * n] += 3
    A[2 * n + 2, 2 * n + 1] -= 3
    return A

N = int(readline())
A = np.array(read().split()).reshape(N, N).astype(np.int32)

def check(A, B):
    N = A.shape[0]
    A = A.ravel()
    B = B.ravel()
    Ax, Ay = np.divmod(np.argmax(A), N)
    Bx, By = np.divmod(np.argmax(B), N)
    pos_A = np.empty_like(A)
    pos_A[A] = np.arange(N * N)
    B = pos_A[B]
    inv = 0
    for i, x in enumerate(B):
        inv += np.sum(B[:i] > x)
    dist = abs(Ax - Bx) + abs(Ay - By)
    return (inv - dist) % 2 == 0

def solve(A):
    N = A.shape[0]
    if N == 1:
        return A
    if N == 2:
        return None
    if N == 3:
        B = f(N) - 1
        if check(A, B):
            return B
        else:
            # おーい、対角線要求されてないんだが
            return np.array([3, 5, 7, 8, 1, 6, 4, 9, 2]).reshape(3, 3) - 1
    if N % 2 == 0:
        B = f(N) - 1
        if check(A, B):
            return B
        else:
            return B[::-1]
    if N % 3 != 0:
        # 完全方陣
        x = np.arange(N)
        B0 = np.add.outer(x, 2 * x) % N
        B1 = B0.T
        B = N * B0 + B1
        if check(A, B):
            return B
        return np.roll(B, 1, axis=0)
    # N は 3 の倍数
    M = N // 3
    C = (f(M) - 1) * 9
    D = f(3) - 1
    B = np.empty_like(A)
    for i in range(3):
        for j in range(3):
            B[i::3, j::3] = C + D[i, j]
    if check(A, B):
        return B
    x, y = divmod(np.argmax(C), M)
    temp = B[3 * x, 3 * y:3 * y + 3].copy()
    B[3 * x, 3 * y:3 * y + 3] = B[3 * x + 2, 3 * y:3 * y + 3]
    B[3 * x + 2, 3 * y:3 * y + 3] = temp
    return B

A[A == 0] = N * N
A -= 1
B = solve(A)
if B is None:
    print('impossible')
else:
    print('possible')
    B += 1
    print('\n'.join(' '.join(row) for row in B.astype(str)))
0