Problem p02579 (python)

Fixed submission:
from collections import deque
import sys


mX = [0, 1, 0, -1]
mY = [1, 0, -1, 0]
zX = [2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2]
zY = [2, 1, 0, -1, -2, 2, 1, -1, -2, 2, -2, 2, -2, 1, -1, 2, 1, 0, -1, -2]


def bfs(u, S, M, H, W):  # u 開始点

    q = deque()
    q.append(u)
    visited = deque()
    while q:
        while q:
            u = q.popleft()
            visited.append([u[0], u[1]])

            # 徒歩移動
            for i in range(4):
                nx, ny = u[0] + mX[i], u[1] + mY[i]

                if nx < 0 or H <= nx or ny < 0 or W <= ny or S[nx][ny] == "#":
                    continue

                if M[nx][ny] == -1 or M[nx][ny] > M[u[0]][u[1]]:  # state を確認
                    M[nx][ny] = M[u[0]][u[1]]  # 徒歩移動
                    q.append([nx, ny])
                    visited.append([nx, ny])



        # 魔法移動
        while visited:
            u = visited.popleft()
            for j in range(20):
                wx = u[0] + zX[j]
                wy = u[1] + zY[j]
                if wx < 0 or H <= wx or wy < 0 or W <= wy or S[wx][wy] == "#":
                    continue
                if M[wx][wy] == -1 or M[wx][wy] > M[u[0]][u[1]] + 1:  # state を確認
                    M[wx][wy] = M[u[0]][u[1]] + 1
                    q.append([wx, wy])


def main():

    H, W = map(int, sys.stdin.readline().rstrip().split())
    Ch, Cw = map(int, sys.stdin.readline().rstrip().split())
    Dh, Dw = map(int, sys.stdin.readline().rstrip().split())

    S = []
    for i in range(H):
        S.append(sys.stdin.readline().rstrip())

    M = [[-1 for _ in range(W)] for _ in range(H)]

    if S[Ch - 1][Cw - 1] == "#":
        print(-1)
        return

    M[Ch - 1][Cw - 1] = 0
    bfs([Ch - 1, Cw - 1], S, M, H, W)

    print(M[Dh - 1][Dw - 1])


main()

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
from numba import njit


mX = [0, 1, 0, -1]
mY = [1, 0, -1, 0]
zX = [2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2]
zY = [2, 1, 0, -1, 2, 2, 1, -1, -2, 2, -2, 2, -2, 1, -1, 2, 1, 0, -1, 2]


@njit
def bfs(u, S, M, H, W):  # u 開始点

    q = deque()
    q.append(u)
    while q:
        u = q.popleft()

        # 徒歩移動
        for i in range(4):
            nx, ny = u[0] + mX[i], u[1] + mY[i]

            if nx < 0 or H <= nx or ny < 0 or W <= ny or S[nx][ny] == "#":
                continue

            if M[nx][ny] == -1 or M[nx][ny] > M[u[0]][u[1]]:  # state を確認
                M[nx][ny] = M[u[0]][u[1]]  # 徒歩移動
                q.append([nx, ny])

        # 魔法移動
        for i in range(-2, 3):
            for j in range(-2, 3):
                wx = u[0] + i
                wy = u[1] + j
                if wx < 0 or H <= wx or wy < 0 or W <= wy or S[wx][wy] == "#":
                    continue
                if M[wx][wy] == -1 or M[wx][wy] > M[u[0]][u[1]] + 1:  # state を確認
                    M[wx][wy] = M[u[0]][u[1]] + 1
                    q.append([wx, wy])


def main():

    H, W = map(int, sys.stdin.readline().rstrip().split())
    Ch, Cw = map(int, sys.stdin.readline().rstrip().split())
    Dh, Dw = map(int, sys.stdin.readline().rstrip().split())

    S = []
    for i in range(H):
        S.append(sys.stdin.readline().rstrip())

    M = [[-1 for _ in range(W)] for _ in range(H)]

    if S[Ch - 1][Cw - 1] == "#":
        print(-1)
        return

    M[Ch - 1][Cw - 1] = 0
    bfs([Ch - 1, Cw - 1], S, M, H, W)

    print(M[Dh - 1][Dw - 1])


main()

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
from numba import njit


mX = [0, 1, 0, -1]
mY = [1, 0, -1, 0]
zX = [2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2]
zY = [2, 1, 0, -1, 2, 2, 1, -1, -2, 2, -2, 2, -2, 1, -1, 2, 1, 0, -1, 2]

@njit
def bfs(u, S, M, H, W):  # u 開始点

    q = deque()
    q.append(u)
    while q:
        u = q.popleft()

        # 徒歩移動
        for i in range(4):
            nx, ny = u[0] + mX[i], u[1] + mY[i]

            if nx < 0 or H <= nx or ny < 0 or W <= ny or S[nx][ny] == "#":
                continue

            if M[nx][ny] == -1 or M[nx][ny] > M[u[0]][u[1]]:  # state を確認
                M[nx][ny] = M[u[0]][u[1]]  # 徒歩移動
                q.append([nx, ny])

        # 魔法移動
        for i in range(20):
            wx = u[0] + zX[i]
            wy = u[1] + zY[i]
            if wx < 0 or H <= wx or wy < 0 or W <= wy or S[wx][wy] == "#":
                continue
            if M[wx][wy] == -1 or M[wx][wy] > M[u[0]][u[1]] + 1:  # state を確認
                M[wx][wy] = M[u[0]][u[1]] + 1
                q.append([wx, wy])


def main():

    H, W = map(int, sys.stdin.readline().rstrip().split())
    Ch, Cw = map(int, sys.stdin.readline().rstrip().split())
    Dh, Dw = map(int, sys.stdin.readline().rstrip().split())

    S = []
    for i in range(H):
        S.append(sys.stdin.readline().rstrip())

    M = [[-1 for _ in range(W)] for _ in range(H)]

    if S[Ch - 1][Cw - 1] == "#":
        print(-1)
        return

    M[Ch - 1][Cw - 1] = 0
    bfs([Ch - 1, Cw - 1], S, M, H, W)

    print(M[Dh - 1][Dw - 1])


main()

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
from namba import njit


mX = [0, 1, 0, -1]
mY = [1, 0, -1, 0]
zX = [2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2]
zY = [2, 1, 0, -1, 2, 2, 1, -1, -2, 2, -2, 2, -2, 1, -1, 2, 1, 0, -1, 2]

@njit
def bfs(u, S, M, H, W):  # u 開始点

    q = deque()
    q.append(u)
    while q:
        u = q.popleft()

        # 徒歩移動
        for i in range(4):
            nx, ny = u[0] + mX[i], u[1] + mY[i]

            if nx < 0 or H <= nx or ny < 0 or W <= ny or S[nx][ny] == "#":
                continue

            if M[nx][ny] == -1 or M[nx][ny] > M[u[0]][u[1]]:  # state を確認
                M[nx][ny] = M[u[0]][u[1]]  # 徒歩移動
                q.append([nx, ny])

        # 魔法移動
        for i in range(20):
            wx = u[0] + zX[i]
            wy = u[1] + zY[i]
            if wx < 0 or H <= wx or wy < 0 or W <= wy or S[wx][wy] == "#":
                continue
            if M[wx][wy] == -1 or M[wx][wy] > M[u[0]][u[1]] + 1:  # state を確認
                M[wx][wy] = M[u[0]][u[1]] + 1
                q.append([wx, wy])


def main():

    H, W = map(int, sys.stdin.readline().rstrip().split())
    Ch, Cw = map(int, sys.stdin.readline().rstrip().split())
    Dh, Dw = map(int, sys.stdin.readline().rstrip().split())

    S = []
    for i in range(H):
        S.append(sys.stdin.readline().rstrip())

    M = [[-1 for _ in range(W)] for _ in range(H)]

    if S[Ch - 1][Cw - 1] == "#":
        print(-1)
        return

    M[Ch - 1][Cw - 1] = 0
    bfs([Ch - 1, Cw - 1], S, M, H, W)

    print(M[Dh - 1][Dw - 1])


main()