Problem p02579 (python)

Fixed submission:

from collections import deque


H, W = [int(x) for x in input().split()]
Ch, Cw = [int(x) - 1 for x in input().split()]
Dh, Dw = [int(x) - 1 for x in input().split()]

warp_list = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
             (-1, -2), (-1, -1), (-1, 1), (-1, 2),
             (0, -2), (0, 2),
             (1, -2), (1, -1), (1, 1), (1, 2),
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

maze = []
warp_count = []
for _ in range(H):
    line = list([x for x in input()])
    maze.append(line)
    warp_count.append([-1] * W)

queue = deque()
warp_queue = deque()

queue.appendleft((Ch, Cw, 0))
warp_count[Ch][Cw] = 0

answer = -1
warp_set = set()
while queue:
    def maze_check(h, w, use_magic):
        if h < 0 or H <= h:
            return False
        if w < 0 or W <= w:
            return False
        if warp_count[h][w] == -1:
            return True
        if warp_count[h][w] <= use_magic:
            return False

    def is_wall(h, w):
        if maze[h][w] == '#':
            return True
        return False

    now_h, now_w, use_magic = queue.pop()

    if now_h == Dh and now_w == Dw:
        if answer == -1:
            answer = use_magic

        elif answer > use_magic:
            answer = use_magic

        continue

    # 上下左右に移動
    for y, x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        if maze_check(now_h + y, now_w + x, use_magic):

            # 上下左右、いずれかに壁がある場合にはワープ先に行く可能性がある
            if is_wall(now_h + y, now_w + x):
                if not (now_h, now_w) in warp_set:
                    warp_queue.appendleft((now_h, now_w, use_magic))
                    warp_set.add((now_h, now_w))
            else:
                queue.appendleft((now_h + y, now_w + x, use_magic))
                warp_count[now_h + y][now_w + x] = use_magic

    if not queue:
        while warp_queue:
            now_h, now_w, use_magic = warp_queue.pop()

            for y, x in warp_list:
                warp_h = now_h + y
                warp_w = now_w + x

                if maze_check(warp_h, warp_w, use_magic+1):
                    if not is_wall(warp_h, warp_w):
                        queue.append((warp_h, warp_w, use_magic+1))
                        warp_count[warp_h][warp_w] = use_magic + 1
        warp_set = set()

print(answer)

Buggy submission (verdict: Runtime Error):

from collections import deque


H, W = [int(x) for x in input().split()]
Ch, Cw = [int(x) - 1 for x in input().split()]
Dh, Dw = [int(x) - 1 for x in input().split()]

warp_list = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
             (-1, -2), (-1, -1), (-1, 1), (-1, 2),
             (0, -2), (0, 2),
             (1, -2), (1, -1), (1, 1), (1, 2),
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

maze = []
warp_count = []
for _ in range(H):
    line = list([x for x in input()])
    maze.append(line)
    warp_count.append([-1] * W)

queue = deque()
warp_queue = deque()

queue.appendleft((Ch, Cw, 0))

answer = -1
while queue:
    def maze_check(h, w, use_magic):
        if h < 0 or H <= h:
            return False
        if w < 0 or W <= w:
            return False
        if maze[h][w] == '#':
            return False
        if warp_count[h][w] == -1:
            return True
        if warp_count[h][w] <= use_magic:
            return False

        return True

    now_h, now_w, use_magic = queue.pop()

    if now_h == Dh and now_w == Dw:

        if answer == -1:
            answer = use_magic

        elif answer > use_magic:
            answer = use_magic

        continue

    # 上下左右に移動
    do_warp = True
    if maze_check(now_h+1, now_w, use_magic):
        queue.appendleft((now_h + 1, now_w, use_magic))
        warp_count[now_h][now_w] = use_magic
        do_warp = False
    if maze_check(now_h-1, now_w, use_magic):
        queue.appendleft((now_h - 1, now_w, use_magic))
        warp_count[now_h - 1][now_w] = use_magic
        do_warp = False
    if maze_check(now_h, now_w+1, use_magic):
        queue.appendleft((now_h, now_w + 1, use_magic))
        warp_count[now_h][now_w + 1] = use_magic
        do_warp = False
    if maze_check(now_h, now_w-1, use_magic):
        queue.appendleft((now_h, now_w - 1, use_magic))
        warp_count[now_h][now_w - 1] = use_magic
        do_warp = False

    if do_warp:
        warp_queue.appendleft((now_h, now_w))

    if not queue:
        while warp_queue:
            warp_h, warp_w, use_magic = warp_queue.pop()

            for y, x in warp_list:
                warp_h += y
                warp_w += x

                if maze_check(warp_h, warp_w, use_magic+1):
                    queue.append((warp_h, warp_w, use_magic+1))
                    warp_count[warp_h][warp_w] = use_magic + 1

print(answer)