結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー zombietan
提出日時 2016-06-01 07:51:01
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 1,109 bytes
コンパイル時間 107 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-10-08 03:44:37
合計ジャッジ時間 1,915 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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