結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー zombietanzombietan
提出日時 2016-06-01 07:51:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 1,109 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-16 22:21:37
合計ジャッジ時間 1,935 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
11,392 KB
testcase_01 AC 36 ms
11,264 KB
testcase_02 AC 35 ms
11,264 KB
testcase_03 AC 47 ms
11,392 KB
testcase_04 AC 34 ms
11,264 KB
testcase_05 AC 33 ms
11,264 KB
testcase_06 AC 38 ms
11,392 KB
testcase_07 AC 37 ms
11,392 KB
testcase_08 AC 42 ms
11,392 KB
testcase_09 AC 43 ms
11,264 KB
testcase_10 AC 37 ms
11,264 KB
testcase_11 AC 39 ms
11,392 KB
testcase_12 AC 47 ms
11,520 KB
testcase_13 AC 46 ms
11,392 KB
testcase_14 AC 42 ms
11,392 KB
testcase_15 AC 46 ms
11,392 KB
testcase_16 AC 44 ms
11,392 KB
testcase_17 AC 42 ms
11,264 KB
testcase_18 AC 47 ms
11,392 KB
testcase_19 AC 42 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from queue import Queue
a = sum([list(map(int, input().split())) for _ in range(4)],[]) 

adjacent = (
    (1, 4),
    (0, 2, 5),
    (1, 3, 6),
    (2, 7),
    (0, 5, 8),
    (1, 4, 6, 9),
    (2, 5, 7, 10),
    (3, 6, 11),
    (4, 9, 12),
    (5, 8, 10, 13),
    (6, 9, 11, 14),
    (7, 10, 15),
    (8, 13),
    (9, 12, 14),
    (10, 13, 15),
    (11, 14)
)

GOAL = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]

class State:
    def __init__(self, board, space, num):
        self.board = board
        self.space = space
        self.num = num

def BFS(start):
    if start == GOAL:
        return 'Yes'
    q = Queue()
    q.put(State(start, start.index(0), []))
    while not q.empty():
        a = q.get()
        for x in adjacent[a.space]:
            b = a.board[:]
            n = a.num[:]
            if b[x] in n:
                continue
            n.append(b[x])
            b[a.space] = b[x]
            b[x] = 0
            c = State(b, x, n)
            if b == GOAL:
                return 'Yes'
            q.put(c)

    return 'No'

if __name__ == '__main__':
    print(BFS(a))
0