結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー cherrypi59cherrypi59
提出日時 2018-06-28 11:20:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 1,310 bytes
コンパイル時間 918 ms
コンパイル使用メモリ 11,108 KB
実行使用メモリ 8,208 KB
最終ジャッジ日時 2023-09-13 14:38:57
合計ジャッジ時間 1,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,084 KB
testcase_01 AC 16 ms
8,164 KB
testcase_02 AC 15 ms
8,000 KB
testcase_03 AC 15 ms
8,040 KB
testcase_04 AC 16 ms
8,048 KB
testcase_05 AC 15 ms
8,120 KB
testcase_06 AC 16 ms
8,048 KB
testcase_07 AC 15 ms
8,068 KB
testcase_08 AC 15 ms
8,048 KB
testcase_09 AC 16 ms
8,136 KB
testcase_10 AC 16 ms
8,036 KB
testcase_11 AC 16 ms
8,036 KB
testcase_12 AC 16 ms
8,208 KB
testcase_13 AC 16 ms
8,136 KB
testcase_14 AC 16 ms
8,048 KB
testcase_15 AC 15 ms
8,092 KB
testcase_16 AC 15 ms
8,168 KB
testcase_17 AC 15 ms
8,040 KB
testcase_18 AC 16 ms
8,056 KB
testcase_19 AC 16 ms
8,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

t = [list(map(int, input().split())) for i in range(4)]

f = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]]
d = [[(0, 0) for j in range(4)] for i in range(4)]


def solve(i, j):
    if j-1 >= 0 and d[i][j-1] == (0, -1):
        d[i][j-1] = (0, 0)
        return solve(i, j-1)
    if j+1 < 4 and d[i][j+1] == (0, 1):
        d[i][j+1] = (0, 0)
        return solve(i, j+1)
    if i-1 >= 0 and d[i-1][j] == (-1, 0):
        d[i-1][j] = (0, 0)
        return solve(i-1, j)
    if i+1 < 4 and d[i+1][j] == (1, 0):
        d[i+1][j] = (0, 0)
        return solve(i+1, j)

    return i, j


def main():
    flg, p = True, (0, 0)
    for i in range(4):
        for j in range(4):
            if f[i][j] == 0:
                p = (i, j)
                continue
            for k in range(4):
                for l in range(4):
                    if f[i][j] == t[k][l]:
                        d[i][j] = (i-k, j-l)
                    if abs(d[i][j][0]) + abs(d[i][j][1]) > 1:
                        flg = False

    pb = solve(3, 3)
    if t[pb[0]][pb[1]] != 0:
        flg = False

    for i in range(4):
        for j in range(4):
            if d[i][j] != (0, 0):
                flg = False

    if flg:
        print("Yes")
    else:
        print("No")


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