Problem p02579 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    from collections import deque
    import sys

    input = sys.stdin.readline
    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    c = [input().rstrip() for _ in range(h)]
    cw -= 1
    ch -= 1
    dw -= 1
    dh -= 1
    c[ch] = c[ch][:cw] + "=" + c[ch][cw + 1 :]
    bfs = deque([[cw, ch]])
    warp_before = set()
    warp_after = set()

    for cnt in range(1000000):  # ワープ回数のカウント
        while bfs:  # ワープせずに歩いてどこまで行けるか
            for _ in range(len(bfs)):
                now_x, now_y = bfs.popleft()
                for next_x, next_y in zip([1, 0, -1, 0], [0, 1, 0, -1]):
                    x, y = now_x + next_x, now_y + next_y
                    if 0 <= x < w and 0 <= y < h:
                        if x == dw and y == dh:
                            print(cnt)
                            exit()
                        c_next = c[y][x]
                        if c_next == "#":  # 壁の隣マスの座標をwarp_beforeに加える
                            warp_before.add((now_x, now_y))
                        if c_next == ".":  # 走査済マーク
                            c[y] = c[y][:x] + "=" + c[y][x + 1 :]
                            bfs.append([x, y])
        else:
            if not warp_before:
                print(-1)
                exit()
            for now_x, now_y in warp_before:
                # 壁の隣マスの座標を中心に5*5マスの範囲で、壁の外側の未到達マスの座標をwarp_afterに加える
                if c[now_y][now_x] == "=":
                    for next_y in range(-2, 3):
                        for next_x in range(-2, 3):
                            x, y = now_x + next_x, now_y + next_y
                            if 0 <= x < w and 0 <= y < h:
                                warp_after.add((x, y))
            for x, y in warp_after:
                # 壁の外側の未到達マスの座標のゴール判定・走査済マーク「=」を付けてbfsに格納
                if c[y][x] == ".":
                    if x == dw and y == dh:  # 終了(ゴール)判定
                        print(cnt + 1)
                        exit()
                    c[y] = c[y][:x] + "=" + c[y][x + 1 :]
                    bfs.append([x, y])
            warp_before = set()
            warp_after = set()


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    from collections import deque
    import sys

    input = sys.stdin.readline
    warp_up = [
        (-2, -2),
        (-1, -2),
        (0, -2),
        (1, -2),
        (2, -2),
        (-2, -1),
        (-1, -1),
        (1, -1),
        (2, -1),
    ]
    warp_do = [
        (2, 2),
        (1, 2),
        (0, 2),
        (-1, 2),
        (-2, 2),
        (2, 1),
        (1, 1),
        (-1, 1),
        (-2, 1),
    ]
    warp_ri = [
        (2, -2),
        (2, -1),
        (2, 0),
        (2, 1),
        (2, 2),
        (1, -2),
        (1, -1),
        (1, 1),
        (1, 2),
    ]
    warp_le = [
        (-2, 2),
        (-2, 1),
        (-2, 0),
        (-2, -1),
        (-2, -2),
        (-1, 2),
        (-1, 1),
        (-1, -1),
        (-1, -2),
    ]

    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    c = [input().rstrip() for _ in range(h)]
    cw -= 1
    ch -= 1
    dw -= 1
    dh -= 1
    c[ch] = c[ch][:cw] + "=" + c[ch][cw + 1 :]
    d1 = deque([[cw, ch]])
    d2 = deque([])

    for cnt in range(30000):
        for _ in range(70000):
            for _ in range(len(d1)):
                now_x, now_y = d1.popleft()
                for next_x, next_y in zip([1, 0, -1, 0], [0, 1, 0, -1]):
                    x, y = now_x + next_x, now_y + next_y
                    if 0 <= x < w and 0 <= y < h:
                        if x == dw and y == dh:
                            print(cnt)
                            exit()
                        c_xy = c[y][x]
                        if c_xy == "#":
                            next = [next_x, next_y]
                            if next == [1, 0]:
                                warp = warp_ri.copy()
                            elif next == [0, 1]:
                                warp = warp_do.copy()
                            elif next == [-1, 0]:
                                warp = warp_le.copy()
                            elif next == [0, -1]:
                                warp = warp_up.copy()
                            for next_x, next_y in warp:
                                x, y = now_x + next_x, now_y + next_y
                                if 0 <= x < w and 0 <= y < h:
                                    if c[y][x] not in "#=!":
                                        c[y] = c[y][:x] + "!" + c[y][x + 1 :]
                                        d2.append([x, y])
                        elif c_xy != "=":
                            c[y] = c[y][:x] + "=" + c[y][x + 1 :]
                            d1.append([x, y])
        else:
            if [dw, dh] in d2:
                print(cnt + 1)
                exit()
            if not d2:
                print(-1)
                exit()
            d1 = d2.copy()
            d2.clear()


if __name__ == "__main__":
    main()