結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー terasaterasa
提出日時 2022-06-05 14:06:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 90 ms / 5,000 ms
コード長 1,695 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,824 KB
実行使用メモリ 76,468 KB
最終ジャッジ日時 2023-10-21 03:10:54
合計ジャッジ時間 2,862 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
75,988 KB
testcase_01 AC 49 ms
55,672 KB
testcase_02 AC 71 ms
70,500 KB
testcase_03 AC 77 ms
73,544 KB
testcase_04 AC 76 ms
72,564 KB
testcase_05 AC 48 ms
55,672 KB
testcase_06 AC 88 ms
76,064 KB
testcase_07 AC 49 ms
55,676 KB
testcase_08 AC 88 ms
76,456 KB
testcase_09 AC 49 ms
55,676 KB
testcase_10 AC 78 ms
72,884 KB
testcase_11 AC 56 ms
63,504 KB
testcase_12 AC 89 ms
75,984 KB
testcase_13 AC 90 ms
76,072 KB
testcase_14 AC 85 ms
76,056 KB
testcase_15 AC 72 ms
72,552 KB
testcase_16 AC 82 ms
74,204 KB
testcase_17 AC 80 ms
73,992 KB
testcase_18 AC 86 ms
75,988 KB
testcase_19 AC 89 ms
76,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


A = [list(map(int, input().split())) for _ in range(4)]
for i in range(4):
    for j in range(4):
        if A[i][j] == 0:
            blank = (i, j)


def dfs(B, blank, moved):
    flag = True
    for i in range(4):
        for j in range(4):
            if A[i][j] != B[i][j]:
                flag = False
                break
    if flag is True:
        print('Yes')
        exit()

    bx, by = blank
    darr = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    for dx, dy in darr:
        nx, ny = bx + dx, by + dy
        if nx < 0 or nx > 3 or ny < 0 or ny > 3:
            continue
        if B[nx][ny] in moved:
            continue
        moved.add(B[nx][ny])
        B[bx][by], B[nx][ny] = B[nx][ny], B[bx][by]
        dfs(B, (nx, ny), moved)
        B[bx][by], B[nx][ny] = B[nx][ny], B[bx][by]
        moved.remove(B[nx][ny])


B = [[4 * i + j + 1 for j in range(4)] for i in range(4)]
B[-1][-1] = 0
dfs(B, (3, 3), set())
print('No')
0